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

Conversation

shindere
Copy link
Contributor

@shindere shindere commented Oct 6, 2020

This PR fixes issue #7677.

When using ocamlc/ocamlopt to compile C files, it is currently not possible
to control where the C compiler should place the resulting object file.

In other words, without this PR, a command like

ocamlopt -c foo.c -o /tmp/foo.o

produced the following error message:

Options -c and -o are incompatible when compiling C files

This PR removes this restriction and lets the compiler accept
that the command-line options -c and -o are given simultaneously
also when compiling one C file, so that the previous command now works.

The -o command-line option remains however forbidden when compiling
several C files during the same compiler invocation.

The PR includes tests to ensure the compiler behaves as expected when
only one C file is provided on the command-line and when several
of them are provided.

A file-exists action has been added to ocamltest to make testing for the
presence of the object file easier.

The code has been tested with precheck, see build
##521.

Cc @dbuenzli

if List.length (List.filter (function
| ProcessCFile _ -> true
| _ -> false) !deferred_actions) > 1 then
fatal "Option -o makes no sense when compiling several C files";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this case should be combined with the one below to reject

ocamlopt -c a.c b.ml -o something

?

@shindere
Copy link
Contributor Author

shindere commented Dec 8, 2020 via email

@shindere
Copy link
Contributor Author

shindere commented Jan 5, 2021

Is there anything that prevents this from being approved / merged?

Would be happy to fix any remaining issue.

Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good and the feature is useful.

The change needs a Changes entry.

@shindere
Copy link
Contributor Author

shindere commented Jan 5, 2021 via email

@gasche
Copy link
Member

gasche commented Jan 5, 2021

Thanks! check-typo complains:

Checking 6f76c388bee0b478dbbb2c2d82e46b389fae66f8: ocamltest/builtin_actions.ml
./ocamltest/builtin_actions.ml:224.35: [white-at-eol] whitespace at end of line
./ocamltest/builtin_actions.ml:226.15: [white-at-eol] whitespace at end of line

@shindere
Copy link
Contributor Author

shindere commented Jan 5, 2021 via email

@gasche
Copy link
Member

gasche commented Jan 5, 2021

check-typo is still complaining in the same way, are you sure that you pushed your fixing change?

When using ocamlc/ocamlopt to compile C files, it was not possible
so far to control where the resulting object file should be written.

In other words, before this commit a command like

ocamlopt -c foo.c -o /tmp/foo.o

produced the following error message:

Options -c and -o are incompatible when compiling C files

This commit removes this restriction and lets the compiler accept
that the command-line options -c and -o are given simultaneously
also when compiling one C file, so that the previous command now works.

The -o command-line option remains forbidden when compiling several source
files.
@shindere
Copy link
Contributor Author

shindere commented Jan 5, 2021 via email

@gasche gasche merged commit 26f6429 into ocaml:trunk Jan 6, 2021
@shindere
Copy link
Contributor Author

shindere commented Jan 6, 2021 via email

@nojb
Copy link
Contributor

nojb commented Jul 30, 2021

There are two issues with this patch:

  1. It broke previous behaviour of ocaml when you don't pass -o:
(before) ocamlc foo.c --> calls "$cc -c foo.c"
(after) ocamlc foo.c --> calls "$cc -o foo.o -c foo.c"

This causes problems when people use:

ocamlc -ccopt '-o whatever' foo.c

which was necessary before this PR.

  1. The -o flag to ocaml{c,opt} is ignored if -c is not passed as well when compiling C files.

@ghost
Copy link

ghost commented Jul 30, 2021

Octachron added a commit that referenced this pull request Sep 9, 2021
Octachron added a commit that referenced this pull request Sep 9, 2021
Fix regression introduced by #9960

(cherry picked from commit cffde31)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants