Skip to content

Commit 94dd826

Browse files
committed
[Driver][SYCL] Consider .lo files as static archives
In certain build situations, *.lo files are created that are static archives. Adjust the detection of static archives to include these, and also perform a more strict check against known file extensions for archives. Signed-off-by: Michael D Toguchi <[email protected]>
1 parent d9de923 commit 94dd826

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
#include "ToolChains/PPCLinux.h"
4242
#include "ToolChains/PS4CPU.h"
4343
#include "ToolChains/RISCVToolchain.h"
44+
#include "ToolChains/SYCL.h"
4445
#include "ToolChains/Solaris.h"
4546
#include "ToolChains/TCE.h"
4647
#include "ToolChains/WebAssembly.h"
4748
#include "ToolChains/XCore.h"
48-
#include "ToolChains/SYCL.h"
4949
#include "clang/Basic/Version.h"
5050
#include "clang/Config/config.h"
5151
#include "clang/Driver/Action.h"
@@ -62,6 +62,7 @@
6262
#include "llvm/ADT/StringExtras.h"
6363
#include "llvm/ADT/StringSet.h"
6464
#include "llvm/ADT/StringSwitch.h"
65+
#include "llvm/BinaryFormat/Magic.h"
6566
#include "llvm/Config/llvm-config.h"
6667
#include "llvm/Option/Arg.h"
6768
#include "llvm/Option/ArgList.h"
@@ -6594,8 +6595,11 @@ bool clang::driver::isStaticArchiveFile(const StringRef &FileName) {
65946595
// Any file with no extension should not be considered an Archive.
65956596
return false;
65966597
StringRef Ext(llvm::sys::path::extension(FileName).drop_front());
6597-
// Only .lib and .a files are to be considered.
6598-
return (Ext == "lib" || Ext == "a");
6598+
llvm::file_magic Magic;
6599+
llvm::identify_magic(FileName, Magic);
6600+
// Only .lib, .a and .lo files are to be considered.
6601+
return (Ext == "lib" ||
6602+
((Ext == "a" || Ext == "lo") && Magic == llvm::file_magic::archive));
65996603
}
66006604

66016605
bool clang::driver::willEmitRemarks(const ArgList &Args) {

clang/test/Driver/sycl-offload-static-lib-2.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,25 @@
1010
// Build a fat static lib that will be used for all tests
1111
// RUN: echo "void foo(void) {}" > %t1.cpp
1212
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl %t1.cpp -c -o %t1_bundle.o
13-
// RUN: llvm-ar crv %t.a %t1_bundle.o
13+
// RUN: llvm-ar cr %t.a %t1_bundle.o
14+
// RUN: llvm-ar cr %t.lo %t1_bundle.o
15+
// RUN: llvm-ar cr %t_2.a %t1_bundle.o
1416
//
1517
// RUN: touch %t.a
16-
// RUN: touch %t.o
1718
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -L/dummy/dir %t.a -### %t.o 2>&1 \
18-
// RUN: | FileCheck %s -check-prefix=STATIC_LIB
19-
// STATIC_LIB: ld{{(.exe)?}}" "-r" "-o" {{.*}} "[[INPUT:.+\.o]]" "-L/dummy/dir"{{.*}} "[[INPUT:.+\.a]]"
20-
// STATIC_LIB: clang-offload-bundler{{.*}} "-type=oo"
21-
// STATIC_LIB: llvm-link{{.*}} "@{{.*}}"
19+
// RUN: | FileCheck %s -check-prefix=STATIC_LIB -DINPUTA=%t.a -DINPUTO=%t.o
20+
// STATIC_LIB: ld{{(.exe)?}}" "-r" "-o" "[[INPUTLD:[^ ]+\.o]]" {{.*}} "-L/dummy/dir"{{.*}} "[[INPUTO]]" "[[INPUTA]]"
21+
// STATIC_LIB: clang-offload-bundler{{.*}} "-type=oo" {{.*}} "-inputs=[[INPUTLD]]" "-outputs=[[LISTFILE:.+\.txt]]"
22+
// STATIC_LIB: llvm-link{{.*}} "@[[LISTFILE]]"
23+
// STATIC_LIB: ld{{.*}} "[[INPUTA]]" "[[INPUTO]]"
24+
25+
// RUN: touch %t.lo
26+
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -L/dummy/dir %t.lo -### %t.o 2>&1 \
27+
// RUN: | FileCheck %s -check-prefix=STATIC_LIB_LO -DINPUTLO=%t.lo -DINPUTO=%t.o
28+
// STATIC_LIB_LO: ld{{(.exe)?}}" "-r" "-o" "[[INPUTLD:[^ ]+\.o]]" {{.*}} "-L/dummy/dir"{{.*}} "[[INPUTO]]" "[[INPUTLO]]"
29+
// STATIC_LIB_LO: clang-offload-bundler{{.*}} "-type=oo" {{.*}} "-inputs=[[INPUTLD]]" "-outputs=[[LISTFILE:.+\.txt]]"
30+
// STATIC_LIB_LO: llvm-link{{.*}} "@[[LISTFILE]]"
31+
// STATIC_LIB_LO: ld{{.*}} "[[INPUTLO]]" "[[INPUTO]]"
2232

2333
/// ###########################################################################
2434

@@ -94,8 +104,11 @@
94104

95105
/// test behaviors of static lib with no source/object
96106
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -L/dummy/dir %t.a -### 2>&1 \
97-
// RUN: | FileCheck %s -check-prefixes=STATIC_LIB_NOSRC
98-
// STATIC_LIB_NOSRC: clang-offload-bundler{{.*}} "-type=ao" "-targets=host-x86_64-unknown-linux-gnu" "-inputs=[[INPUTLIB:.+\.a]]" "-check-section"
107+
// RUN: | FileCheck %s -check-prefixes=STATIC_LIB_NOSRC,STATIC_LIB_NOSRC_A
108+
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -L/dummy/dir %t.lo -### 2>&1 \
109+
// RUN: | FileCheck %s -check-prefixes=STATIC_LIB_NOSRC,STATIC_LIB_NOSRC_LO
110+
// STATIC_LIB_NOSRC_A: clang-offload-bundler{{.*}} "-type=ao" "-targets=host-x86_64-unknown-linux-gnu" "-inputs=[[INPUTLIB:.+\.a]]" "-check-section"
111+
// STATIC_LIB_NOSRC_LO: clang-offload-bundler{{.*}} "-type=ao" "-targets=host-x86_64-unknown-linux-gnu" "-inputs=[[INPUTLIB:.+\.lo]]" "-check-section"
99112
// STATIC_LIB_NOSRC: ld{{.*}} "-r" "-o" "[[PARTIALOBJ:.+\.o]]" "{{.*}}crt1.o" {{.*}} "-L/dummy/dir" {{.*}} "[[INPUTLIB]]"
100113
// STATIC_LIB_NOSRC: clang-offload-bundler{{.*}} "-type=oo" "-targets=sycl-spir64-unknown-unknown-sycldevice" "-inputs=[[PARTIALOBJ]]" "-outputs=[[DEVICELIST:.+\.txt]]" "-unbundle"
101114
// STATIC_LIB_NOSRC: llvm-link{{.*}} "@[[DEVICELIST]]" "-o" "[[BCFILE:.+\.bc]]"

clang/test/Driver/sycl-offload-static-lib.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
// REQUIRES: clang-driver
55
// REQUIRES: x86-registered-target
66

7+
/// test behaviors of passing a fat static lib
8+
// Build a fat static lib that will be used for all tests
9+
// RUN: echo "void foo(void) {}" > %t1.cpp
10+
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl %t1.cpp -c -o %t1_bundle.o
11+
// RUN: llvm-ar cr %t.a %t1_bundle.o
12+
// RUN: llvm-ar cr %t.lo %t1_bundle.o
13+
// RUN: llvm-ar cr %t_2.a %t1_bundle.o
14+
715
/// ###########################################################################
816

917
/// test behaviors of -foffload-static-lib=<lib>

0 commit comments

Comments
 (0)