Skip to content

Commit d694594

Browse files
committed
[clang][IFS] Allow 2 output files when using -o and -c with clang IFS stubs.
This patch allows for -o to be used with -c when compiling with clang interface stubs enabled. This is because the second file will be an intermediate ifs stubs file that is the text stub analog of the .o file. Both get produces in this case, so two files. Why are we doing this? Because we want to support the case where interface stubs are used bu first invoking clang like so: clang -c <other flags> -emit-interface-stubs foo.c -o foo.o ... clang -emit-interface-stubs <.o files> -o libfoo.so This should generate N .ifs files, and one .ifso file. Prior to this patch, using -o with the -c invocation was not possible. Currently the clang driver supports generating a a.out/.so file at the same time as a merged ifs file / ifso file, but this is done by checking that the final job is the IfsMerge job. When -c is used, the final job is a Compile job so what this patch does is check to figure out of the job type is TY_IFS_CPP. Differential Revision: https://reviews.llvm.org/D70763
1 parent ec71238 commit d694594

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,16 +3689,27 @@ void Driver::BuildJobs(Compilation &C) const {
36893689
Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
36903690

36913691
// It is an error to provide a -o option if we are making multiple output
3692-
// files. There is one exception, IfsMergeJob: when generating interface stubs
3693-
// enabled we want to be able to generate the stub file at the same time that
3694-
// we generate the real library/a.out. So when a .o, .so, etc are the output,
3695-
// with clang interface stubs there will also be a .ifs and .ifso at the same
3696-
// location.
3692+
// files. There are exceptions:
3693+
//
3694+
// IfsMergeJob: when generating interface stubs enabled we want to be able to
3695+
// generate the stub file at the same time that we generate the real
3696+
// library/a.out. So when a .o, .so, etc are the output, with clang interface
3697+
// stubs there will also be a .ifs and .ifso at the same location.
3698+
//
3699+
// CompileJob of type TY_IFS_CPP: when generating interface stubs is enabled
3700+
// and -c is passed, we still want to be able to generate a .ifs file while
3701+
// we are also generating .o files. So we allow more than one output file in
3702+
// this case as well.
3703+
//
36973704
if (FinalOutput) {
36983705
unsigned NumOutputs = 0;
3706+
unsigned NumIfsOutputs = 0;
36993707
for (const Action *A : C.getActions())
37003708
if (A->getType() != types::TY_Nothing &&
37013709
!(A->getKind() == Action::IfsMergeJobClass ||
3710+
(A->getType() == clang::driver::types::TY_IFS_CPP &&
3711+
A->getKind() == clang::driver::Action::CompileJobClass &&
3712+
0 == NumIfsOutputs++) ||
37023713
(A->getKind() == Action::BindArchClass && A->getInputs().size() &&
37033714
A->getInputs().front()->getKind() == Action::IfsMergeJobClass)))
37043715
++NumOutputs;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5828,8 +5828,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
58285828
if (Output.getType() == types::TY_Dependencies) {
58295829
// Handled with other dependency code.
58305830
} else if (Output.isFilename()) {
5831-
CmdArgs.push_back("-o");
5832-
CmdArgs.push_back(Output.getFilename());
5831+
if (Output.getType() == clang::driver::types::TY_IFS_CPP ||
5832+
Output.getType() == clang::driver::types::TY_IFS) {
5833+
SmallString<128> OutputFilename(Output.getFilename());
5834+
llvm::sys::path::replace_extension(OutputFilename, "ifs");
5835+
CmdArgs.push_back("-o");
5836+
CmdArgs.push_back(Args.MakeArgString(OutputFilename));
5837+
} else {
5838+
CmdArgs.push_back("-o");
5839+
CmdArgs.push_back(Output.getFilename());
5840+
}
58335841
} else {
58345842
assert(Output.isNothing() && "Invalid output.");
58355843
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// REQUIRES: x86-registered-target
2+
// REQUIRES: shell
3+
4+
// RUN: mkdir -p %t; cd %t
5+
// RUN: %clang -target x86_64-unknown-linux-gnu -c -emit-interface-stubs %s -o %t/driver-test3.o
6+
// RUN: llvm-nm %t/driver-test3.o | FileCheck --check-prefix=CHECK-OBJ %s
7+
// RUN: cat %t/driver-test3.ifs | FileCheck --check-prefix=CHECK-IFS %s
8+
9+
// CHECK-OBJ: bar
10+
11+
// CHECK-IFS: --- !experimental-ifs-v1
12+
// CHECK-IFS-NEXT: IfsVersion:
13+
// CHECK-IFS-NEXT: Triple:
14+
// CHECK-IFS-NEXT: ObjectFileFormat:
15+
// CHECK-IFS-NEXT: Symbols:
16+
// CHECK-IFS-NEXT: "bar" : { Type: Func }
17+
// CHECK-IFS-NEXT: ...
18+
19+
int bar(int a) { return a; }

0 commit comments

Comments
 (0)