-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlakefile.lean
More file actions
94 lines (77 loc) · 3.24 KB
/
lakefile.lean
File metadata and controls
94 lines (77 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import Lake
open System Lake DSL
/-- Split a shell-style flag string on spaces and drop empties. -/
def splitFlags (s : String) : Array String :=
s.splitOn " " |>.filter (· ≠ "") |>.toArray
/-- Run `pkg-config` and split the output into flags. Returns `#[]` on failure. -/
def pkgConfig (pkg : String) (flag : String) : IO (Array String) := do
let out ← IO.Process.output { cmd := "pkg-config", args := #[flag, pkg] }
if out.exitCode != 0 then return #[]
return splitFlags out.stdout.trimAscii.toString
/-- Run `xcrun --show-sdk-path` and return the SDK path on Apple platforms. -/
def macSdkPath : IO (Option FilePath) := do
let out ← IO.Process.output { cmd := "xcrun", args := #["--show-sdk-path"] }
if out.exitCode != 0 then
return none
else
return some out.stdout.trimAscii.toString
/-- Prefer an explicit linker override when supplied by the environment. -/
def zlibLdFlagsOverride : IO (Option (Array String)) := do
return (← IO.getEnv "ZLIB_LDFLAGS") |>.map (splitFlags ·.trimAscii.toString)
/-- Get zlib include flags, respecting `ZLIB_CFLAGS` env var override. -/
def zlibCFlags : IO (Array String) := do
if let some flags := (← IO.getEnv "ZLIB_CFLAGS") then
return splitFlags flags.trimAscii.toString
let flags ← pkgConfig "zlib" "--cflags"
if !flags.isEmpty then
return flags
if let some sdk := (← macSdkPath) then
return #["-I", (sdk / "usr/include").toString]
return #[]
/-- Extract `-L` library paths from `NIX_LDFLAGS` (set by nix-shell). -/
def nixLdLibPaths : IO (Array String) := do
let some val := (← IO.getEnv "NIX_LDFLAGS") | return #[]
return val.splitOn " " |>.filter (·.startsWith "-L") |>.toArray
/-- Get link flags for zlib.
Tries `ZLIB_LDFLAGS`, then pkg-config, then macOS SDK / Nix fallbacks. -/
def linkFlags : IO (Array String) := do
if let some flags := (← zlibLdFlagsOverride) then
return flags
let libPaths ← nixLdLibPaths
let zlibFlags ← pkgConfig "zlib" "--libs"
if !zlibFlags.isEmpty && zlibFlags.any (·.startsWith "-L") then
return zlibFlags
if let some sdk := (← macSdkPath) then
return #["-L", (sdk / "usr/lib").toString, "-lz"]
if !zlibFlags.isEmpty then
return libPaths ++ zlibFlags
-- pkg-config unavailable — try NIX_LDFLAGS for -L paths
return libPaths ++ #["-lz"]
package «lean-zip» where
moreLinkArgs := run_io linkFlags
testDriver := "test"
require zipCommon from git "https://github.com/kim-em/lean-zip-common" @ "89204d61227069f5c5d19dc69418ab57f96fe61c"
lean_lib Zip
-- zlib FFI
input_file zlib_ffi.c where
path := "c" / "zlib_ffi.c"
text := true
target zlib_ffi.o pkg : FilePath := do
let srcJob ← zlib_ffi.c.fetch
let oFile := pkg.buildDir / "c" / "zlib_ffi.o"
let weakArgs := #["-I", (← getLeanIncludeDir).toString] ++ (← zlibCFlags)
let hardArgs := if Platform.isWindows then #[] else #["-fPIC"]
buildO oFile srcJob weakArgs hardArgs "cc"
extern_lib libzlib_ffi pkg := do
let ffiO ← zlib_ffi.o.fetch
let name := nameToStaticLib "zlib_ffi"
buildStaticLib (pkg.staticLibDir / name) #[ffiO]
lean_lib ZipTest where
globs := #[.submodules `ZipTest]
@[default_target]
lean_exe test where
root := `ZipTest
lean_exe bench where
root := `ZipBench
lean_exe fuzz_inflate where
root := `ZipFuzzInflate