Skip to content

Commit

Permalink
Add support for alwayslink
Browse files Browse the repository at this point in the history
  • Loading branch information
brentleyjones committed Jun 29, 2022
1 parent c61c578 commit 5bf9b61
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 29 deletions.
1 change: 1 addition & 0 deletions examples/cc/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cc_library(
# Simulating a "local_includes"
copts = local_includes("private"),
local_defines = ["SECRET_3=\\\"Hello\\\""],
alwayslink = True,
)

cc_library(
Expand Down
1 change: 1 addition & 0 deletions examples/command_line/third_party/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ apple_static_framework_import(
sdk_frameworks = ["Foundation"],
visibility = ["//visibility:public"],
weak_sdk_frameworks = ["SwiftUI"],
alwayslink = True,
)
12 changes: 7 additions & 5 deletions test/fixtures/cc/bwb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@
/* Begin PBXFileReference section */
1FA9CAC061B4F491CEBDF60A /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
26866FEDF67CD3FA66FFAB17 /* lib.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = lib.c; sourceTree = "<group>"; };
295286D1006239978AF51215 /* liblib_impl.lo */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.lo; path = "bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.lo"; sourceTree = BUILT_PRODUCTS_DIR; };
2F2B12BDC30B75C3CD2012BD /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
3C80F439DD6B6A9F76C8BCC6 /* private.inc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = private.inc; sourceTree = "<group>"; };
3FEB5348BF9F16CDB97FB486 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3FEB5348BF9F16CDB97FB486 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib2/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
43CB090CE80B33F7C6C1C7F6 /* lib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lib.h; sourceTree = "<group>"; };
5CEC67C55F51736AB9F17063 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/external/examples_cc_external/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5F5A15B1B3110ED9696BD6FB /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
82E8E5D354CD9B769C1DD508 /* lib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lib.h; sourceTree = "<group>"; };
8D73636373B8CDD4BE62C0A5 /* private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = private.h; sourceTree = "<group>"; };
8EEF31F83F9AF990435CC135 /* tool */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = tool; sourceTree = BUILT_PRODUCTS_DIR; };
9411DCDC5C6F92122299107F /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
CDA701F96A5177E4188BEA69 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib2/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
CDA701F96A5177E4188BEA69 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/external/examples_cc_external/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
E179886B32A8D20BA0FD517F /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
F360F9D030E464AA774319E0 /* lib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lib.h; sourceTree = "<group>"; };
FB0F020B344E60F89B31265D /* lib.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = lib.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -149,7 +149,7 @@
children = (
3FEB5348BF9F16CDB97FB486 /* liblib_impl.a */,
CDA701F96A5177E4188BEA69 /* liblib_impl.a */,
5CEC67C55F51736AB9F17063 /* liblib_impl.a */,
295286D1006239978AF51215 /* liblib_impl.lo */,
8EEF31F83F9AF990435CC135 /* tool */,
);
name = Products;
Expand Down Expand Up @@ -555,7 +555,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "c++0x";
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_BITCODE = NO;
EXECUTABLE_EXTENSION = a;
EXECUTABLE_EXTENSION = lo;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"_FORTIFY_SOURCE=1",
Expand Down Expand Up @@ -671,6 +671,8 @@
"-lc++",
"-filelist",
"$(INTERNAL_DIR)/targets/darwin_x86_64-dbg-ST-5534cb307cb8/examples/cc/tool/tool.LinkFileList",
"-force_load",
"$(BUILD_DIR)/bazel-out/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.lo",
);
PRODUCT_MODULE_NAME = _tool_Stub;
PRODUCT_NAME = tool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
test/fixtures/cc/bwb.xcodeproj/rules_xcodeproj/links/gen_dir/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.a
test/fixtures/cc/bwb.xcodeproj/rules_xcodeproj/links/gen_dir/darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib2/liblib_impl.a
test/fixtures/cc/bwb.xcodeproj/rules_xcodeproj/links/gen_dir/darwin_x86_64-dbg-ST-5534cb307cb8/bin/external/examples_cc_external/liblib_impl.a
12 changes: 7 additions & 5 deletions test/fixtures/cc/bwb_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"product": {
"name": "lib_impl",
"path": {
"_": "darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.a",
"_": "darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.lo",
"t": "g"
},
"type": "com.apple.product-type.library.static"
Expand Down Expand Up @@ -272,17 +272,19 @@
"is_swift": false,
"label": "//examples/cc/tool:tool",
"linker_inputs": {
"force_load": [
{
"_": "darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.lo",
"t": "g"
}
],
"linkopts": [
"-lc++",
"-headerpad_max_install_names",
"-no-canonical-prefixes",
"-lc++"
],
"static_libraries": [
{
"_": "darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib/liblib_impl.a",
"t": "g"
},
{
"_": "darwin_x86_64-dbg-ST-5534cb307cb8/bin/examples/cc/lib2/liblib_impl.a",
"t": "g"
Expand Down
12 changes: 7 additions & 5 deletions test/fixtures/cc/bwx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
01934C980471E67330AC657C /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/external/examples_cc_external/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
0282BFF13FDF4BED1663D2AF /* lib.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = lib.c; sourceTree = "<group>"; };
054732C7DCF543EED5337D81 /* private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = private.h; sourceTree = "<group>"; };
19B81294B60CEC2BA6843380 /* lib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lib.h; sourceTree = "<group>"; };
1E24825205C7C82ED0184DEA /* tool */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = tool; sourceTree = BUILT_PRODUCTS_DIR; };
31620B8F4712C7D8A23356B9 /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
473752B2F4FD8D5B513230B3 /* private.inc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = private.inc; sourceTree = "<group>"; };
4C8095547A55E1C477C2E094 /* lib.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = lib.c; sourceTree = "<group>"; };
61D4E93D586B426B1BE67C73 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
61D4E93D586B426B1BE67C73 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib2/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
72DD4925E3FB6AE32B7ACADA /* lib.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = lib.c; sourceTree = "<group>"; };
887E70CEDC5A85E62FB23126 /* lib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lib.h; sourceTree = "<group>"; };
8C467A9CF11D6BFFA7FEE894 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib2/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
8C467A9CF11D6BFFA7FEE894 /* liblib_impl.a */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.a; path = "bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/external/examples_cc_external/liblib_impl.a"; sourceTree = BUILT_PRODUCTS_DIR; };
921187174A58741CF8ADCCD9 /* lib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lib.h; sourceTree = "<group>"; };
BD77ACEF34FC7AFD41A09A4A /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
C0FE22BC231A9DB0A7E546A8 /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
DD6E27CD14BF041559DD9225 /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
EA9A40D0AB233B5E56E5E050 /* liblib_impl.lo */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; name = liblib_impl.lo; path = "bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.lo"; sourceTree = BUILT_PRODUCTS_DIR; };
F836536D759D1084EDC9B80D /* BUILD */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = BUILD; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -131,7 +131,7 @@
children = (
61D4E93D586B426B1BE67C73 /* liblib_impl.a */,
8C467A9CF11D6BFFA7FEE894 /* liblib_impl.a */,
01934C980471E67330AC657C /* liblib_impl.a */,
EA9A40D0AB233B5E56E5E050 /* liblib_impl.lo */,
1E24825205C7C82ED0184DEA /* tool */,
);
name = Products;
Expand Down Expand Up @@ -476,7 +476,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "c++0x";
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_BITCODE = NO;
EXECUTABLE_EXTENSION = a;
EXECUTABLE_EXTENSION = lo;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"_FORTIFY_SOURCE=1",
Expand Down Expand Up @@ -690,6 +690,8 @@
"-lc++",
"-filelist",
"$(INTERNAL_DIR)/targets/darwin_x86_64-dbg-ST-ccd9595da841/examples/cc/tool/tool.LinkFileList",
"-force_load",
"$(BUILD_DIR)/bazel-out/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.lo",
);
PRODUCT_MODULE_NAME = _tool_Stub;
PRODUCT_NAME = tool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
test/fixtures/cc/bwx.xcodeproj/rules_xcodeproj/links/gen_dir/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.a
test/fixtures/cc/bwx.xcodeproj/rules_xcodeproj/links/gen_dir/darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib2/liblib_impl.a
test/fixtures/cc/bwx.xcodeproj/rules_xcodeproj/links/gen_dir/darwin_x86_64-dbg-ST-ccd9595da841/bin/external/examples_cc_external/liblib_impl.a
12 changes: 7 additions & 5 deletions test/fixtures/cc/bwx_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"product": {
"name": "lib_impl",
"path": {
"_": "darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.a",
"_": "darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.lo",
"t": "g"
},
"type": "com.apple.product-type.library.static"
Expand Down Expand Up @@ -272,17 +272,19 @@
"is_swift": false,
"label": "//examples/cc/tool:tool",
"linker_inputs": {
"force_load": [
{
"_": "darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.lo",
"t": "g"
}
],
"linkopts": [
"-lc++",
"-headerpad_max_install_names",
"-no-canonical-prefixes",
"-lc++"
],
"static_libraries": [
{
"_": "darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib/liblib_impl.a",
"t": "g"
},
{
"_": "darwin_x86_64-dbg-ST-ccd9595da841/bin/examples/cc/lib2/liblib_impl.a",
"t": "g"
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/command_line/bwb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,8 @@
"-no-canonical-prefixes",
"-filelist",
"$(INTERNAL_DIR)/targets/macos-x86_64-min11.0-applebin_macos-darwin_x86_64-dbg-ST-0139d977e630/examples/command_line/tool/tool.LinkFileList",
"-force_load",
"$(PROJECT_DIR)/examples/command_line/third_party/ExampleFramework.framework/ExampleFramework",
);
PRODUCT_NAME = tool;
SDKROOT = macosx;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/command_line/bwb_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@
"t": "e"
}
],
"force_load": [
"examples/command_line/third_party/ExampleFramework.framework/ExampleFramework"
],
"linkopts": [
"-Wl,-add_ast_path,$(BUILD_DIR)/bazel-out/macos-x86_64-min11.0-applebin_macos-darwin_x86_64-dbg-ST-0139d977e630/bin/examples/command_line/lib/LibSwift.swiftmodule/x86_64-apple-macos.swiftmodule",
"-L$(DEVELOPER_DIR)/Platforms/MacOSX.platform/Developer/usr/lib",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/command_line/bwx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,8 @@
"-no-canonical-prefixes",
"-filelist",
"$(INTERNAL_DIR)/targets/macos-x86_64-min11.0-applebin_macos-darwin_x86_64-dbg-ST-59a3e2f8ef60/examples/command_line/tool/tool.LinkFileList",
"-force_load",
"$(PROJECT_DIR)/examples/command_line/third_party/ExampleFramework.framework/ExampleFramework",
);
PRODUCT_NAME = tool;
SDKROOT = macosx;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/command_line/bwx_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@
"t": "e"
}
],
"force_load": [
"examples/command_line/third_party/ExampleFramework.framework/ExampleFramework"
],
"linkopts": [
"-Wl,-add_ast_path,$(BUILD_DIR)/bazel-out/macos-x86_64-min11.0-applebin_macos-darwin_x86_64-dbg-ST-59a3e2f8ef60/bin/examples/command_line/lib/LibSwift.swiftmodule/x86_64-apple-macos.swiftmodule",
"-L$(DEVELOPER_DIR)/Platforms/MacOSX.platform/Developer/usr/lib",
Expand Down
6 changes: 6 additions & 0 deletions tools/generator/src/DTO/LinkerInputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ struct LinkerInputs: Equatable {
let staticFrameworks: [FilePath]
let dynamicFrameworks: [FilePath]
var staticLibraries: OrderedSet<FilePath>
var forceLoad: OrderedSet<FilePath>
let linkopts: [String]

init(
staticFrameworks: [FilePath] = [],
dynamicFrameworks: [FilePath] = [],
staticLibraries: OrderedSet<FilePath> = [],
forceLoad: OrderedSet<FilePath> = [],
linkopts: [String] = []
) {
self.staticFrameworks = staticFrameworks
self.dynamicFrameworks = dynamicFrameworks
self.staticLibraries = staticLibraries
self.forceLoad = forceLoad
self.linkopts = linkopts
}
}
Expand All @@ -24,6 +27,7 @@ extension LinkerInputs {
return Set(staticFrameworks.filter { $0.type != .generated })
.union(Set(dynamicFrameworks.filter { $0.type != .generated }))
.union(Set(staticLibraries.filter { $0.type != .generated }))
.union(Set(forceLoad.filter { $0.type != .generated }))
}
}

Expand All @@ -34,6 +38,7 @@ extension LinkerInputs: Decodable {
case staticFrameworks
case dynamicFrameworks
case staticLibraries
case forceLoad
case linkopts
}

Expand All @@ -43,6 +48,7 @@ extension LinkerInputs: Decodable {
staticFrameworks = try container.decodeFilePaths(.staticFrameworks)
dynamicFrameworks = try container.decodeFilePaths(.dynamicFrameworks)
staticLibraries = try container.decodeFilePaths(.staticLibraries)
forceLoad = try container.decodeFilePaths(.forceLoad)
linkopts = try container
.decodeIfPresent([String].self, forKey: .linkopts) ?? []
}
Expand Down
1 change: 1 addition & 0 deletions tools/generator/src/Generator/ProcessTargetMerges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ exist

// Update linker inputs
merged.linkerInputs.staticLibraries.remove(merging.product.path)
merged.linkerInputs.forceLoad.remove(merging.product.path)

// Update dependencies
merged.dependencies.formUnion(merging.dependencies)
Expand Down
10 changes: 10 additions & 0 deletions tools/generator/src/Generator/SetTargetConfigurations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ Target with id "\(id)" not found in `consolidatedTarget.uniqueFiles`
.joined(separator: " ")
)

let forceLoadLibraries = target.linkerInputs.forceLoad
if !forceLoadLibraries.isEmpty {
try buildSettings.prepend(
onKey: "OTHER_LDFLAGS",
forceLoadLibraries.flatMap { filePath in
return ["-force_load", try filePathResolver.resolve(filePath).string.quoted]
}
)
}

if !target.linkerInputs.staticLibraries.isEmpty {
let linkFileList = try filePathResolver
.resolve(try target.linkFileListFilePath())
Expand Down
Loading

0 comments on commit 5bf9b61

Please sign in to comment.