Skip to content
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

Extend -o to work when compiling C files #9960

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Working version

### Compiler user-interface and warnings:

- #9960: extend ocamlc/ocamlopt's -o option to work when compiling C files
(Sébastien Hinderer, reported by Daniel Bünzli, review by Florian
Angeletti and Gabriel Scherer)

### Internal/compiler-libs changes:

- #9650, #9651: keep refactoring the pattern-matching compiler
Expand Down
17 changes: 9 additions & 8 deletions driver/compenv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,13 @@ let process_action
| ProcessCFile name ->
readenv ppf (Before_compile name);
Location.input_name := name;
if Ccomp.compile_file name <> 0 then raise (Exit_with_status 2);
ccobjs := c_object_of_filename name :: !ccobjs
let obj_name = match !output_name with
| None -> c_object_of_filename name
| Some n -> n
in
if Ccomp.compile_file ~output:obj_name name <> 0
then raise (Exit_with_status 2);
ccobjs := obj_name :: !ccobjs
| ProcessObjects names ->
ccobjs := names @ !ccobjs
| ProcessDLLs names ->
Expand Down Expand Up @@ -687,14 +692,10 @@ let process_deferred_actions env =
begin
match final_output_name with
| None -> ()
| Some output_name ->
| Some _output_name ->
if !compile_only then begin
if List.filter (function
| ProcessCFile name -> c_object_of_filename name <> output_name
| _ -> false) !deferred_actions <> [] then
fatal "Options -c and -o are incompatible when compiling C files";

if List.length (List.filter (function
| ProcessCFile _
| ProcessImplementation _
| ProcessInterface _ -> true
| _ -> false) !deferred_actions) > 1 then
Expand Down
26 changes: 25 additions & 1 deletion ocamltest/builtin_actions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,29 @@ let check_program_output = make
Builtin_variables.output
Builtin_variables.reference)

let file_exists_action _log env =
match Environments.lookup Builtin_variables.file env with
| None ->
let reason = reason_with_fallback env "the file variable is undefined" in
let result = Result.fail_with_reason reason in
(result, env)
| Some filename ->
if Sys.file_exists filename
then begin
let default_reason = Printf.sprintf "File %s exists" filename in
let reason = reason_with_fallback env default_reason in
let result = Result.pass_with_reason reason in
(result, env)
end else begin
let default_reason =
Printf.sprintf "File %s does not exist" filename
in
let reason = reason_with_fallback env default_reason in
let result = Result.fail_with_reason reason in
(result, env)
end
let file_exists = make "file-exists" file_exists_action

let initialize_test_exit_status_variables _log env =
Environments.add_bindings
[
Expand Down Expand Up @@ -263,5 +286,6 @@ let _ =
arch_i386;
arch_power;
function_sections;
naked_pointers
naked_pointers;
file_exists;
]
2 changes: 2 additions & 0 deletions ocamltest/builtin_actions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ val run : Actions.t
val script : Actions.t

val check_program_output : Actions.t

val file_exists : Actions.t
4 changes: 4 additions & 0 deletions ocamltest/builtin_variables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ let commandline = Variables.make ("commandline",
let exit_status = Variables.make ("exit_status",
"Expected program exit status")

let file = Variables.make ("file",
"File whose existence should be tested")

let files = Variables.make ("files",
"Files used by the tests")

Expand Down Expand Up @@ -109,6 +112,7 @@ let _ = List.iter Variables.register_variable
cwd;
commandline;
exit_status;
file;
files;
make;
ocamltest_response;
Expand Down
2 changes: 2 additions & 0 deletions ocamltest/builtin_variables.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ val commandline : Variables.t

val exit_status : Variables.t

val file : Variables.t

val files : Variables.t

val make : Variables.t
Expand Down
8 changes: 8 additions & 0 deletions testsuite/tests/tool-command-line/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Hello, world!\n");
return 0;
}
18 changes: 18 additions & 0 deletions testsuite/tests/tool-command-line/test-o-one-c-file.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(* TEST
files = "hello.c"
* setup-ocamlopt.opt-build-env
** script
script = "mkdir outputdir"
*** ocamlopt.opt
all_modules = "hello.c"
compile_only = "true"
flags = "-o outputdir/hello.${objext}"
**** file-exists
file = "outputdir/hello.${objext}"
*)

(*
This test makes sure it is possible to specify the name of the output
object file when compiling a C file with the OCaml compiler.
The test does not need to contain any OCaml code.
*)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Options -c -o are incompatible with compiling multiple files
15 changes: 15 additions & 0 deletions testsuite/tests/tool-command-line/test-o-several-files.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(* TEST
* setup-ocamlopt.opt-build-env
** ocamlopt.opt
all_modules = "foo.c bar.c"
compile_only = "true"
flags = "-o outputdir/baz.${objext}"
ocamlopt_opt_exit_status = "2"
*** check-ocamlopt.opt-output
*)

(*
This test makes sure that the -o option is rejected when trying to
compile several C files during the same invocatin of the OCaml compiler.
The test does not need to contain any OCaml code.
*)
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ compiler_output = "compiler-output"

*)

(* this file is just a test driver, the test does not contain real OCamlcode *)
(*
This file is just a test driver, the test does not contain any
real OCaml code
*)