Skip to content

Commit

Permalink
[Patmos][Clang] Can now compile only object or archive files without …
Browse files Browse the repository at this point in the history
…any .c files.
  • Loading branch information
Emoun committed Jan 20, 2025
1 parent 4b3976b commit 23dc100
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
28 changes: 16 additions & 12 deletions clang/lib/Driver/ToolChains/Patmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ void patmos::PatmosBaseTool::PrepareLink1Inputs(

void patmos::PatmosBaseTool::PrepareLink2Inputs(
const llvm::opt::ArgList &Args,
const char* Input,
llvm::Optional<const char*> Input,
llvm::opt::ArgStringList &LinkInputs) const
{
LinkInputs.push_back(Args.MakeArgString(getLibPath("lib/crt0.o")));
LinkInputs.push_back(Args.MakeArgString(getLibPath("lib/crtbegin.o")));
LinkInputs.push_back(Args.MakeArgString(getLibPath("lib/crtend.o")));

LinkInputs.push_back(Input);
if(Input) LinkInputs.push_back(*Input);

// We hide symbols to allow redefinition of stdlib symbols without
// clashing with stdlib
Expand Down Expand Up @@ -673,20 +673,25 @@ void patmos::Compile::ConstructJob(Compilation &C, const JobAction &JA,
}
}



void patmos::FinalLink::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
const ArgList &Args,
const char *LinkingOutput) const
{
//////////////////////////////////////////////////////////////////////////////
// build LINK 1 command
const char *link1Out = CreateOutputFilename(C, Output, "link-", "bc", false);
ArgStringList LinkInputs;
PrepareLink1Inputs(Args, Inputs, LinkInputs);
ConstructLLVMLinkJob(*this, C, JA, Output, Inputs, link1Out, LinkInputs, Args);
auto any_c_files = std::any_of(Args.begin(), Args.end(), [&](auto arg){
return isInputFileArg(arg) && !(isObjectFileArg(arg) || isArchiveFileArg(arg));
});

llvm::Optional<const char*> link1Out = llvm::None;
if(any_c_files) {
//////////////////////////////////////////////////////////////////////////////
// build LINK 1 command
link1Out = CreateOutputFilename(C, Output, "link-", "bc", false);
ArgStringList LinkInputs;
PrepareLink1Inputs(Args, Inputs, LinkInputs);
ConstructLLVMLinkJob(*this, C, JA, Output, Inputs, *link1Out, LinkInputs, Args);
}

//////////////////////////////////////////////////////////////////////////////
// build LINK 2 command
Expand Down Expand Up @@ -721,14 +726,13 @@ void patmos::FinalLink::ConstructJob(Compilation &C, const JobAction &JA,
ConstructLLVMLinkJob(*this, C, JA, Output, Inputs, link4Out, Link4Inputs, Args);

////////////////////////////////////////////////////////////////////////////
// build LLC command
// build LLC and LLD commands
const char *llcOut = CreateOutputFilename(C, Output, "llc-", "bc", false);
ConstructLLCJob(*this, C, JA, Output, Inputs, llcOut,
link4Out, Args);

ArgStringList LLDInputs;
LLDInputs.push_back(llcOut);

/// Add object files to link
for(auto arg: Args) {
if(isObjectFileArg(arg) || isArchiveFileArg(arg)) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Patmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PatmosBaseTool {
const InputInfoList &Inputs,
llvm::opt::ArgStringList &LinkInputs) const;
void PrepareLink2Inputs(const llvm::opt::ArgList &Args,
const char* Input,
llvm::Optional<const char*> Input,
llvm::opt::ArgStringList &LinkInputs) const;
void PrepareLink3Inputs(const llvm::opt::ArgList &Args,
const char* Input,
Expand Down
6 changes: 5 additions & 1 deletion clang/test/Driver/Patmos/compile-and-link-archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
// RUN: %clang --target=patmos %S/helpers/helper-function2.c -c -o %t-object2.o
// RUN: ar cr %t-archive.a %t-object.o %t-object2.o
// RUN: %clang --target=patmos %s %t-archive.a -o %t
// RUN: llvm-objdump -rd %t | FileCheck %s
// END.
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tests can add object file to compile command
// Tests can add archive file to compile command
//
///////////////////////////////////////////////////////////////////////////////////////////////////

// CHECK-DAG: <helper_source_function>
extern int helper_source_function(int x);
// CHECK-DAG: <helper_source_function2>
extern int helper_source_function2(int x);

// CHECK-DAG: <main>
int main() {
return helper_source_function2(helper_source_function(0));
}
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/Patmos/compile-and-link-object.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// RUN: %clang --target=patmos %S/helpers/helper-function.c -c -o %t-object.o
// RUN: %clang --target=patmos %s %t-object.o -o %t
// RUN: llvm-objdump -rd %t | FileCheck %s
// END.
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tests can add object file to compile command
//
///////////////////////////////////////////////////////////////////////////////////////////////////

// CHECK-DAG: <helper_source_function>
extern int helper_source_function(int x);

// CHECK-DAG: <main>
int main() {
return helper_source_function(0);
}
Expand Down
23 changes: 23 additions & 0 deletions clang/test/Driver/Patmos/compile-only-archives.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %clang --target=patmos %S/helpers/helper-function.c -c -o %t-object.o
// RUN: %clang --target=patmos %S/helpers/helper-function2.c -c -o %t-object2.o
// RUN: ar cr %t-archive.a %t-object.o %t-object2.o
// RUN: %clang --target=patmos %s -c -o %t-object3.o
// RUN: ar cr %t-archive2.a %t-object3.o
// RUN: %clang -v --target=patmos %t-archive.a %t-archive2.a -o %t
// RUN: llvm-objdump -rd %t | FileCheck %s
// END.
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tests can compile only archive files
//
///////////////////////////////////////////////////////////////////////////////////////////////////

// CHECK-DAG: <helper_source_function>
extern int helper_source_function(int x);
// CHECK-DAG: <helper_source_function2>
extern int helper_source_function2(int x);

// CHECK-DAG: <main>
int main(int x) {
return helper_source_function(helper_source_function2(x));
}
14 changes: 14 additions & 0 deletions clang/test/Driver/Patmos/compile-only-objects.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang --target=patmos %S/helpers/helper-main.c -c -o %t-object.o
// RUN: %clang --target=patmos %S/helpers/helper-function.c -c -o %t-object2.o
// RUN: %clang --target=patmos %t-object.o %t-object2.o -o %t
// RUN: llvm-objdump -rd %t | FileCheck %s
// END.
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tests can compile only an object files
//
///////////////////////////////////////////////////////////////////////////////////////////////////

// CHECK-DAG: <main>
// CHECK-DAG: <helper_source_function>

10 changes: 10 additions & 0 deletions clang/test/Driver/Patmos/helpers/helper-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// This is a helper .c file that isn't itself a test.
//
///////////////////////////////////////////////////////////////////////////////////////////////////
volatile int HELPER_SOURCE_INT_1 = 1;

int main() {
return HELPER_SOURCE_INT_1;
}

0 comments on commit 23dc100

Please sign in to comment.