-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use leanc
written in Lean for testing & distribution
#687
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
def main (args : List String) : IO UInt32 := do | ||
if args.isEmpty then | ||
IO.println "Lean C compiler | ||
|
||
A simple wrapper around a C compiler. Defaults to `@LEANC_CC@`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is cool, are you moving bash shell scripts to lean? That's awesome, and will make one day building on windows easier right? Do we still need nix if we use Lean like this? Is this what Lake is ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
which can be overridden with the environment variable `LEAN_CC`. All parameters are passed | ||
as-is to the wrapped compiler. | ||
|
||
Interesting options: | ||
* `-U LEAN_MULTI_THREAD` can be used to optimize programs not making use of multi-threading | ||
* `--print-cflags`: print C compiler flags necessary for building against the Lean runtime and exit | ||
* `--print-ldlags`: print C compiler flags necessary for statically linking against the Lean library and exit | ||
* Set the `LEANC_GMP` environment variable to a path to `libgmp.a` (or `-l:libgmp.a` on Linux) to link GMP statically. | ||
Beware of the licensing consequences since GMP is LGPL." | ||
return 1 | ||
|
||
let binDir ← IO.appDir | ||
-- Zig gets confused by relative include paths on Windows | ||
let binDir ← IO.FS.realPath binDir | ||
let root := binDir.parent.get! | ||
-- We assume that the CMake variables do not contain escaped spaces | ||
let cflags := ["-I", (root / "include").toString] ++ "@LEANC_EXTRA_FLAGS@".trim.splitOn | ||
let mut ldflags := ["-L", (root / "lib").toString, "-L", (root / "lib" / "lean").toString, (← IO.getEnv "LEANC_GMP").getD "-lgmp"] ++ "@LEAN_EXTRA_LINKER_FLAGS@".trim.splitOn | ||
let mut ldflagsExt := "@LEANC_STATIC_LINKER_FLAGS@".trim.splitOn | ||
|
||
for arg in args do | ||
match arg with | ||
| "-shared" => | ||
-- switch to shared linker flags | ||
ldflagsExt := "@LEANC_SHARED_LINKER_FLAGS@".trim.splitOn | ||
| "-c" => | ||
ldflags := [] | ||
ldflagsExt := [] | ||
| "--print-cflags" => | ||
IO.println <| " ".intercalate cflags | ||
return 0 | ||
| "--print-ldflags" => | ||
IO.println <| " ".intercalate (cflags ++ ldflagsExt ++ ldflags) | ||
return 0 | ||
| _ => () | ||
|
||
let mut cc := (← IO.getEnv "LEAN_CC").getD "@LEANC_CC@" | ||
if cc.startsWith "." then | ||
cc := (binDir / cc).toString | ||
|
||
let args := cflags ++ args ++ ldflagsExt ++ ldflags ++ ["-Wno-unused-command-line-argument"] | ||
if args.contains "-v" then | ||
IO.eprintln s!"{cc} {" ".intercalate args}" | ||
let child ← IO.Process.spawn { cmd := cc, args := args.toArray } | ||
child.wait |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,4 @@ | ||
#!/usr/bin/env bash | ||
# Lean compiler | ||
# | ||
# A simple wrapper around a C compiler. Defaults to `@LEANC_CC@`, | ||
# which can be overridden with the environment variable `LEAN_CC`. All parameters are passed | ||
# as-is to the wrapped compiler. | ||
# | ||
# Interesting options: | ||
# * `-U LEAN_MULTI_THREAD` can be used to optimize programs not making use of multi-threading | ||
# * `--print-cflags`: print C compiler flags necessary for building against the Lean runtime and exit | ||
# * `--print-ldlags`: print C compiler flags necessary for statically linking against the Lean library and exit | ||
# * Set the `LEANC_GMP` environment variable to a path to `libgmp.a` (or `-l:libgmp.a` on Linux) to link GMP statically. | ||
# Beware of the licensing consequences since GMP is LGPL. | ||
|
||
set -e | ||
bindir=$(dirname $0) | ||
|
||
cflags=("-I$bindir/../include" @LEANC_EXTRA_FLAGS@) | ||
ldflags=("-L$bindir/../lib/lean" "${LEANC_GMP:--lgmp}" @LEAN_EXTRA_LINKER_FLAGS@) | ||
ldflags_ext=(@LEANC_STATIC_LINKER_FLAGS@) | ||
args=("$@") | ||
for arg in "$@"; do | ||
case $arg in | ||
-shared) | ||
# switch to shared linker flags | ||
ldflags_ext=(@LEANC_SHARED_LINKER_FLAGS@) | ||
;; | ||
-lleanshared | *libleanshared.*) | ||
# linking against libleanshared explicitly (or linking libleanshared itself) ~> do not link against static stdlib | ||
ldflags_ext=() | ||
;; | ||
-c) | ||
ldflags=() | ||
ldflags_ext=() | ||
;; | ||
--print-cflags) | ||
echo "${cflags[@]}" | ||
exit | ||
;; | ||
--print-ldflags) | ||
echo "${cflags[@]} ${ldflags_ext[@]} ${ldflags[@]}" | ||
exit | ||
;; | ||
esac | ||
done | ||
|
||
[ -n "$LEAN_CC" ] || LEAN_CC="@LEANC_CC@" | ||
|
||
$LEAN_CC "${cflags[@]}" "${args[@]}" "${ldflags_ext[@]}" "${ldflags[@]}" -Wno-unused-command-line-argument | ||
# used only for building Lean itself | ||
root=$(dirname $0) | ||
${LEAN_CC:-@CMAKE_C_COMPILER@} "-I$root/include" @LEANC_EXTRA_FLAGS@ "$@" "-L$root/lib/lean" "${LEANC_GMP:--lgmp}" @LEAN_EXTRA_LINKER_FLAGS@ -Wno-unused-command-line-argument | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really still need this leanc.sh then? Can't we just move this line into the place that calls it? I think that's only stdlib.make.in right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could, yes. It wouldn't exactly be pretty, and it makes it easier to communicate the flags to Nix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why cant this library dependency be a cmake target DEPENDS ? is it because "leanshared" is not a cmake "add_library" somewhere? If so, why isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This flag is baked into
leanc
, i.e. user programs are linked with it as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way the title of this PR confuses me, "testing & distribution", isn't stdlib.make a central part of the regular build too? What does "distribution" mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Distribution means what we ship to users, i.e. the release tarballs created by
cpack