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

Call linker even when Clflags.objfiles is empty #8877

Merged
merged 2 commits into from Feb 22, 2021

Conversation

gretay-js
Copy link
Contributor

I've encountered a surprising behavior of ocamlopt: it silently ignores some input .o and .a files, when compilation is done separately from linking. Here is how it happens:

$ ls
test.ml
$ ocamlopt test.ml -c
$ ocamlopt test.o -o test.exe
$ ls test.exe
<what do you expect? well, test.exe is not there!! >

It doesn't happen when at least one of the inputs is cmx or cmxa.

The linker is not called, because there are no input files to compile. That is, optmain discovers that Clflags.objfiles is empty and doesn't call link. The problem is that some link targets are in Clflags.ccobjs not in Clflags.objfiles.

This issue can cause missing files go unnoticed. For example, the following command returns successfully (i.e., with exit code 0), even when there is no t.o file:

ocamlopt t.o -o test.exe -verbose

It does not report an error, as would happen with other input file types, such as .ml and .cmx. Of course, it does not generate test.exe either.

Normally, .o and .a files are passed to the linker directly, without checking if the files exist, because the linker would fail with an error if they don't. However, in the above example, the linker was not called, because there were no input files to compile.

This one line PR removes the check that Clflags.objfiles is not empty before link. I am not entirely sure that this won't have bad consequences, but it seems that asmlink works just fine with an empty Clflags.objfiles. Tests pass.

@xavierleroy
Copy link
Contributor

This one line PR removes the check that Clflags.objfiles is not empty before link

IIRC, the motivation for this check was to make sure that if the user types just ocamlopt, it doesn't create an a.out executable that does nothing. Whatever you do, don't break this behavior.

@gretay-js
Copy link
Contributor Author

Sadly, I broke this behavior.

Here is another take on it: keep track of whether linker inputs were specified on command line. A minor downside is that it won't take into account object files sneakily passed using -cclib. It seems reasonable, considering that -cclib is intended to pass options to the linker, not files.

Record when linker inputs are specified on command line.
@gretay-js
Copy link
Contributor Author

I've rebased this PR.
It's a small change that fixes ocamlopt for the case when it is called with .o or .a files only as inputs.

Copy link
Contributor

@lpw25 lpw25 left a comment

Choose a reason for hiding this comment

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

LGTM

@lpw25 lpw25 merged commit d674d20 into ocaml:trunk Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants