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

feat: add linkopts property #678

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/androidbp_cclibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func addCompilableProps(mod bpwriter.Module, m Compilable, ctx blueprint.ModuleC
m.FlagsInTransitive(ctx).ForEachIf(
func(f flag.Flag) bool {
// Includes are not part of cflags on AOSP, and the exporting of them is done by Soong itself,
// hence exclude processing of includes here and handle it seperately.
// hence exclude processing of includes here and handle it separately.
return !f.MatchesType(flag.TypeInclude)
},
func(f flag.Flag) {
Expand All @@ -432,6 +432,8 @@ func addCompilableProps(mod bpwriter.Module, m Compilable, ctx blueprint.ModuleC
cxxFlags = append(cxxFlags, f.ToString())
case f.MatchesType(flag.TypeLinker):
ldflags = append(ldflags, f.ToString())
case f.MatchesType(flag.TypeTransitiveLinker):
ldflags = append(ldflags, f.ToString())
case f.MatchesType(flag.TypeAsm):
asflags = append(asflags, f.ToString())
}
Expand All @@ -452,7 +454,7 @@ func addCompilableProps(mod bpwriter.Module, m Compilable, ctx blueprint.ModuleC
},
)

// Check if any inclues are being exported
// Check if any includes are being exported
m.FlagsOut().ForEachIf(
func(f flag.Flag) bool {
return f.MatchesType(flag.TypeInclude) &&
Expand Down
1 change: 1 addition & 0 deletions core/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
TypeCpp
TypeCC
TypeLinker
TypeTransitiveLinker // Only for strict targets to comply with Bazel
TypeLinkLibrary
TypeInclude
TypeIncludeLocal // Helper flag to mark local include dirs
Expand Down
9 changes: 9 additions & 0 deletions core/linux_cclibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ func (g *linuxGenerator) getCommonLibArgs(m BackendCommonLibraryInterface, ctx b
return f.MatchesType(flag.TypeLinkLibrary)
}).ToStringSlice()

m.FlagsInTransitive(ctx).ForEachIf(
func(f flag.Flag) bool {
return f.MatchesType(flag.TypeTransitiveLinker)
},
func(f flag.Flag) {
ldlibs = append(ldlibs, f.ToString())
},
)

if m.IsForwardingSharedLibrary() {
ldflags = append(ldflags, tc.GetLinker().KeepUnusedDependencies())
} else {
Expand Down
35 changes: 35 additions & 0 deletions core/strict_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"github.com/ARM-software/bob-build/core/file"
"github.com/ARM-software/bob-build/core/flag"
"github.com/ARM-software/bob-build/core/tag"
"github.com/google/blueprint"
)

Expand All @@ -24,6 +26,39 @@ func (m *ModuleStrictBinary) OutFiles() file.Paths {
}
}

func (m *ModuleStrictBinary) FlagsInTransitive(ctx blueprint.BaseModuleContext) flag.Flags {

flags := m.ModuleStrictLibrary.FlagsInTransitive(ctx)

visited := map[string]bool{}

ctx.VisitDirectDepsIf(
func(dep blueprint.Module) bool {
name := ctx.OtherModuleName(dep)
if _, ok := visited[name]; !ok && ctx.OtherModuleDependencyTag(dep) == tag.SharedTag {
visited[name] = true
return visited[name]
}

return false
},
func(child blueprint.Module) {
if provider, ok := child.(flag.Provider); ok {
linkopts := provider.FlagsOut().Filtered(
func(f flag.Flag) bool {
return f.MatchesType(flag.TypeTransitiveLinker)
},
)

linkopts.ForEach(func(f flag.Flag) {
flags = flags.AppendIfUnique(f)
})
}
})

return flags
}

func (m *ModuleStrictBinary) GenerateBuildActions(ctx blueprint.ModuleContext) {
if isEnabled(m) {
getGenerator(ctx).strictBinaryActions(m, ctx)
Expand Down
12 changes: 12 additions & 0 deletions core/strict_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type StrictLibraryProps struct {
Alwayslink *bool
Linkstatic *bool

Linkopts []string

Local_defines []string
Copts []string
Deps []string
Expand Down Expand Up @@ -160,6 +162,11 @@ func (m *ModuleStrictLibrary) FlagsIn() flag.Flags {
Tag: flag.TypeIncludeLocal | flag.TypeInclude,
Factory: flag.FromIncludePathOwned,
},
{
PropertyName: "Linkopts",
Tag: flag.TypeTransitiveLinker,
Factory: flag.FromStringOwned,
},
}

return flag.ParseFromProperties(nil, lut, m.Properties)
Expand Down Expand Up @@ -191,6 +198,11 @@ func (m *ModuleStrictLibrary) FlagsOut() flag.Flags {
Tag: flag.TypeIncludeLocal | flag.TypeExported | flag.TypeTransitive | flag.TypeIncludeSystem,
Factory: flag.FromIncludePathOwned,
},
{
PropertyName: "Linkopts",
Tag: flag.TypeTransitiveLinker,
Factory: flag.FromStringOwned,
},
}

return flag.ParseFromProperties(nil, lut, m.Properties)
Expand Down
3 changes: 2 additions & 1 deletion docs/module_types/bob_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```bp
bob_library {
name, srcs, hdrs, copts, local_defines, defines, deps
name, srcs, hdrs, copts, local_defines, defines, deps, linkopts
}
```

Expand All @@ -21,3 +21,4 @@ This target replaces `bob_static_library` & `bob_shared_library` to mimic Bazel
| `local_defines` | List of strings; default is `[]`<br>Defines that are local to the module and are not added to modules that depend upon this. |
| `copts` | List of strings; default is `[]`<br>This options are included as cflags in the compile/link commands. |
| `deps` | List of targets; default is `[]`<br>The list of other libraries to be linked in to the binary target. |
| [`linkopts`](properties/linkopts.md) | List of strings; default is `[]`<br>List of additional flags to the linker command. |
3 changes: 2 additions & 1 deletion docs/module_types/bob_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```bp
bob_test {
name, srcs, hdrs, copts, deps, tags
name, srcs, hdrs, copts, deps, tags, linkopts
}
```

Expand All @@ -26,3 +26,4 @@ Supports:
| `copts` | List of strings; default is `[]`<br>This options are included as cflags in the compile/link commands. |
| `deps` | List of targets; default is `[]`<br>The list of other libraries to be linked in to the binary target. |
| [`tags`](properties/common_properties.md#tags) | List of strings; default is `[]` |
| [`linkopts`](properties/linkopts.md) | List of strings; default is `[]`<br>List of additional flags to the linker command. |
35 changes: 35 additions & 0 deletions docs/module_types/properties/linkopts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Additional linker options

`linkopts` property allows to provide additional linker options. However there is no any filtering or checking for them - they are passed as is.

Depending on the backend, linker options may behave a bit different thus need to be used carefully. Check below how they are applied for particular backends.
If needed, they can be separated with [features](../../features.md).

## Example

```bp
bob_library {
name: "libname",
srcs: ["libname.cpp"],

builder_android_bp: {
linkopts: [
"-u fake_android",
],
},

builder_ninja: {
linkopts: [
"-u fake_linux",
],
},
}
```

## Linux Backend

`linkopts` options are appended to the end of the linker command in the order specified.

## Android backend

`linkopts` options are directly mapped to Android module's `ldflags` property.
Empty file.
1 change: 1 addition & 0 deletions gendiffer/tests/executable/linkopts/app/bplist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build.bp
36 changes: 36 additions & 0 deletions gendiffer/tests/executable/linkopts/app/build.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
bob_library {
name: "libA",
srcs: ["libA.cpp"],
host_supported: true,
build_by_default: true,
linkopts: [
"-lfoo",
"-Wl,--use-me,$$VAR",
],
}

bob_library {
name: "libB",
srcs: ["libB.cpp"],
host_supported: true,
build_by_default: true,
linkstatic: false,
linkopts: [
"-lbar",
"-Wl,-Map,output.map",
],
}

bob_executable {
name: "hello",
srcs: ["hello.cpp"],
deps: [
"libA",
"libB",
],
linkopts: [
"-Wl,-Fake,fakeing",
],
host_supported: true,
build_by_default: true,
}
Empty file.
86 changes: 86 additions & 0 deletions gendiffer/tests/executable/linkopts/out/android/Android.bp.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

genrule {
name: "_check_buildbp_updates_redacted",
srcs: ["build.bp"],
out: ["androidbp_up_to_date"],
tool_files: ["scripts/verify_hash.py"],
cmd: "python $(location scripts/verify_hash.py) --hash redacted --out $(out) -- $(in)",
}

cc_binary_host {
name: "hello__host",
stem: "hello",
srcs: ["hello.cpp"],
ldflags: [
"-Wl,-Fake,fakeing",
"-lbar",
"-Wl,-Map,output.map",
],
shared_libs: ["libB__host"],
static_libs: ["libA__host"],
}

cc_binary {
name: "hello__target",
stem: "hello",
srcs: ["hello.cpp"],
ldflags: [
"-Wl,-Fake,fakeing",
"-lbar",
"-Wl,-Map,output.map",
],
shared_libs: ["libB__target"],
static_libs: ["libA__target"],
compile_multilib: "both",
}

cc_library {
name: "libA__host",
host_supported: true,
device_supported: false,
stem: "libA",
srcs: ["libA.cpp"],
ldflags: [
"-lfoo",
"-Wl,--use-me,$$VAR",
],
}

cc_library {
name: "libA__target",
host_supported: false,
device_supported: true,
stem: "libA",
srcs: ["libA.cpp"],
ldflags: [
"-lfoo",
"-Wl,--use-me,$$VAR",
],
compile_multilib: "both",
}

cc_library {
name: "libB__host",
host_supported: true,
device_supported: false,
stem: "libB",
srcs: ["libB.cpp"],
ldflags: [
"-lbar",
"-Wl,-Map,output.map",
],
}

cc_library {
name: "libB__target",
host_supported: false,
device_supported: true,
stem: "libB",
srcs: ["libB.cpp"],
ldflags: [
"-lbar",
"-Wl,-Map,output.map",
],
compile_multilib: "both",
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
Empty file.
Loading
Loading