-
-
Notifications
You must be signed in to change notification settings - Fork 124
feat(compiler)!: Out of source builds #2384
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
base: oscar/gc-rebased
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -1,11 +1,21 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const commander = require("commander"); | ||
| const path = require("path"); | ||
| const exec = require("./exec.js"); | ||
| const pkgJson = require("../package.json"); | ||
|
|
||
| const stdlibPath = require("@grain/stdlib"); | ||
|
|
||
| function defaultWasmLocation(file, options) { | ||
| const targetDir = options.targetDir | ||
| ? path.resolve(options.targetDir) | ||
| : path.resolve("target"); | ||
| const profile = options.release ? "release" : "debug"; | ||
| const basename = path.basename(file).replace(/\.gr$/, ".wasm"); | ||
| return path.join(targetDir, profile, basename); | ||
| } | ||
|
|
||
| function list(val) { | ||
| return val.split(","); | ||
| } | ||
|
|
@@ -89,12 +99,23 @@ class GrainCommand extends commander.Command { | |
| list, | ||
| [], | ||
| ); | ||
| cmd.forwardOption( | ||
| "-L, --library <libs>", | ||
| "load libraries: -L name1=dir1,name2=dir2", | ||
| list, | ||
| [], | ||
| ); | ||
| cmd.forwardOption( | ||
| "-S, --stdlib <path>", | ||
| "override the standard library with your own", | ||
| null, | ||
| stdlibPath, | ||
| ); | ||
| cmd.forwardOption( | ||
| "--target-dir <dir>", | ||
| "directory where build artifacts are written", | ||
| ); | ||
| cmd.forwardOption("--project-root <dir>", "project root directory"); | ||
| cmd.forwardOption( | ||
| "--initial-memory-pages <size>", | ||
| "initial number of WebAssembly memory pages", | ||
|
|
@@ -180,7 +201,7 @@ program | |
| .action(function (file, options, program) { | ||
| const success = exec.grainc(file, options, program); | ||
| if (success) { | ||
| const outFile = options.o ?? file.replace(/\.gr$/, ".wasm"); | ||
| const outFile = options.o ?? defaultWasmLocation(file, options); | ||
|
Member
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. How would we feel about setting I think this provides a nicer user experience with the js api having the wasm file just be there.
Member
Author
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. I think it's better to not have the file be in the source directory by default. |
||
| exec.grainrun(unprocessedArgs, outFile, options, program); | ||
| } | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,6 @@ open Grain; | |
| open Grain_typed; | ||
| open Compile; | ||
| open Printf; | ||
| open Lexing; | ||
| open Filename; | ||
| open Cmdliner; | ||
|
|
||
| let () = | ||
|
|
@@ -49,36 +47,33 @@ let error_wrapped = f => | |
| exit(2); | ||
| }; | ||
|
|
||
| let compile_file = (~outfile=?, filename) => { | ||
| let outfile = | ||
| Option.value( | ||
| ~default=Compile.default_object_filename(filename), | ||
| outfile, | ||
| ); | ||
| ignore(Compile.compile_file(~outfile, filename)); | ||
| }; | ||
| let compile_file = (~outfile=?, filename) => | ||
| error_wrapped(() => compile_file(~outfile?, filename)); | ||
| let compile_file = (~object_outfile=?, filename) => | ||
| error_wrapped(() => | ||
| ignore(Compile.compile_file(~object_outfile?, filename)) | ||
| ); | ||
|
|
||
| let grainc = (single_file_mode, name, outfile) => { | ||
| let grainc = (single_file_mode, input_path, object_outfile) => { | ||
| Grain_utils.Config.set_root_config(); | ||
|
|
||
| if (!Printexc.backtrace_status() && Grain_utils.Config.verbose^) { | ||
| Printexc.record_backtrace(true); | ||
| }; | ||
|
|
||
| let name = Fp.toString(input_path); | ||
|
|
||
| if (single_file_mode) { | ||
| compile_file(~outfile?, name); | ||
| compile_file(~object_outfile?, name); | ||
| } else { | ||
| switch (Grain_utils.Config.wasi_polyfill^) { | ||
| | Some(name) => | ||
| | Some(polyfill) => | ||
| let poyfill = Fp.toString(polyfill); | ||
| Grain_utils.Config.preserve_config(() => { | ||
| Grain_utils.Config.compilation_mode := Grain_utils.Config.Runtime; | ||
| Module_resolution.load_dependency_graph(name); | ||
| Module_resolution.load_dependency_graph(poyfill); | ||
| let to_compile = Module_resolution.get_out_of_date_dependencies(); | ||
| List.iter(compile_file, to_compile); | ||
| compile_file(name); | ||
| }) | ||
| compile_file(poyfill); | ||
| }); | ||
| | None => () | ||
| }; | ||
|
|
||
|
|
@@ -90,21 +85,20 @@ let grainc = (single_file_mode, name, outfile) => { | |
| if (Grain_utils.Config.statically_link^) { | ||
| let main_object = Compile.default_object_filename(name); | ||
| let outfile = | ||
| Option.value(~default=Compile.default_wasm_filename(name), outfile); | ||
| Option.value( | ||
| ~default=Compile.default_wasm_filename(name), | ||
| object_outfile, | ||
| ); | ||
| let dependencies = Module_resolution.get_dependencies(); | ||
|
|
||
| error_wrapped(() => Link.link(~main_object, ~outfile, dependencies)); | ||
| }; | ||
| }; | ||
|
|
||
| `Ok(); | ||
| }; | ||
|
|
||
| /** Converter which checks that the given output filename is valid */ | ||
|
Member
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. Why did we remove this comment? The function still looks todo the same thing? |
||
|
|
||
| let output_file_conv = { | ||
| let parse = s => { | ||
| let s_dir = dirname(s); | ||
| let s_dir = Filename.dirname(s); | ||
| Sys.file_exists(s_dir) | ||
| ? if (Sys.is_directory(s_dir)) { | ||
| `Ok(s); | ||
|
|
@@ -116,19 +110,17 @@ let output_file_conv = { | |
| (parse, Format.pp_print_string); | ||
| }; | ||
|
|
||
| let input_file_conv = { | ||
| open Arg; | ||
| let (prsr, prntr) = non_dir_file; | ||
|
|
||
| (filename => prsr(filename), prntr); | ||
| }; | ||
|
|
||
| let input_filename = { | ||
| let doc = sprintf("Grain source file to compile"); | ||
| let docv = "FILE"; | ||
| Arg.( | ||
| required | ||
| & pos(~rev=true, 0, some(input_file_conv), None) | ||
| & pos( | ||
| ~rev=true, | ||
| 0, | ||
| some(Grain_utils.Filepath.Args.ExistingFile.cmdliner_converter), | ||
| None, | ||
| ) | ||
| & info([], ~docv, ~doc) | ||
| ); | ||
| }; | ||
|
|
@@ -156,12 +148,10 @@ let cmd = { | |
| Cmd.v( | ||
| Cmd.info(Sys.argv[0], ~version, ~doc), | ||
| Term.( | ||
| ret( | ||
| Grain_utils.Config.with_cli_options(grainc) | ||
| $ single_file_mode | ||
| $ input_filename | ||
| $ output_filename, | ||
| ) | ||
| Grain_utils.Config.with_cli_options(grainc) | ||
| $ single_file_mode | ||
| $ input_filename | ||
| $ output_filename | ||
| ), | ||
| ); | ||
| }; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Does this pr allow us to drop the
pkg.jsfile in the cli?It only exists to map the build outputs to a target directory but we are doing that inside the compiler now?
(Also just remembered that we are going to need to update the playground to handle the out of source stuff before we release this version if we merge this).