Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 0745246

Browse files
yottatsafacebook-github-bot
authored andcommitted
support c_shared mode
Summary: Split from D29875545: This add c_shared and c_archive mode which produces either .so or .a file instead of binary. The code is ported to the modern buck. Reviewed By: lebentle fbshipit-source-id: bb7cee0ed586f463d93f8335dcb32e532bb10ad8
1 parent 499f461 commit 0745246

15 files changed

Lines changed: 407 additions & 15 deletions

docs/__go_common.soy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Buck currently supports Go version 1.10.
137137
{param default: '[]' /}
138138
{param desc}
139139
Extra external linker flags passed to go link via <code>-extld</code> argument.
140-
If argument is non-empty or <code>cgo_library</code> is used, the link mode
140+
If argument is non-empty or <code>cgo_library</code> is used, the link mode
141141
will switch to <code>external</code>.
142142
{/param}
143143
{/call}

docs/__table_of_contents.soy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
'go_library',
7777
'go_test',
7878
'cgo_library',
79+
'go_exported_library',
80+
'prebuilt_go_library',
7981
],
8082
'groovy': [
8183
'groovy_library',

docs/rule/go_exported_library.soy

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
{namespace go_exported_library}
18+
19+
/***/
20+
{template .soyweb}
21+
{call buck.page}
22+
{param title: 'go_exported_library()' /}
23+
{param navid: 'rule_go_exported_library' /}
24+
{param prettify: true /}
25+
{param description}
26+
A go_exported_library() rule builds a Go code into C shared or static library
27+
{/param}
28+
{param content}
29+
30+
{call buck.rule}
31+
{param status: 'UNFROZEN' /}
32+
{param overview}
33+
<p>
34+
A go_exported_library() rule builds a C library from the supplied set of Go source files
35+
and dependencies. This is done via <code>-buildmode</code> flag and "//export" annotations in the code.
36+
</p>
37+
<p>
38+
{call go_common.supported_language_version /}
39+
</p>
40+
{/param}
41+
42+
{param args}
43+
44+
{call buck.arg}
45+
{param name: 'name' /}
46+
{param desc}
47+
The name of the rule.
48+
{/param}
49+
{/call}
50+
51+
{call go_common.srcs_arg /}
52+
53+
{call go_common.deps_arg /}
54+
55+
{call buck.arg}
56+
{param name : 'build_mode' /}
57+
{param desc}
58+
Determines the build mode (equivalent of <code>-buildmode</code>). Can be
59+
one of the following values: <code>c_archive</code>, <code>c_shared</code>.
60+
This argument is valid only if at there is at least one <code>cgo_library</deps> declared in deps.
61+
In addition you should make sure that <code>-shared</code> flag is added to <code>compiler_flags</code>
62+
and go version under <code>go.goroot</code> is compiled with that flag present in:
63+
<code>gcflags</code>, <code>ldflags</code> and <code>asmflags</code>
64+
{/param}
65+
{/call}
66+
67+
{call go_common.link_style_arg /}
68+
69+
{call go_common.link_mode_arg /}
70+
71+
{call go_common.compiler_flags_arg /}
72+
73+
{call go_common.assembler_flags_arg /}
74+
75+
{call go_common.linker_flags_arg /}
76+
77+
{call go_common.external_linker_flags_arg /}
78+
79+
{call buck.arg}
80+
{param name: 'resources' /}
81+
{param default : '[]' /}
82+
{param desc}
83+
Static files to be symlinked into the working directory of the test. You can access these in your
84+
by opening the files as relative paths, e.g. <code>ioutil.ReadFile("testdata/input")</code>.
85+
{/param}
86+
{/call}
87+
88+
{/param} // close args
89+
90+
{param examples}
91+
92+
{call go_common.more_examples /}
93+
94+
{literal}<pre class="prettyprint lang-py">
95+
go_exported_library(
96+
name = "shared",
97+
srcs = ["main.go"],
98+
build_mode = "c_shared",
99+
compiler_flags = ["-shared"],
100+
deps = [":example"],
101+
)
102+
103+
cgo_library(
104+
name = "example",
105+
package_name = "cgo",
106+
srcs = [
107+
"export-to-c.go", # file with //export annotations
108+
],
109+
cgo_compiler_flags = [],
110+
compiler_flags = [],
111+
headers = [],
112+
)
113+
114+
cxx_genrule(
115+
name = "cgo_exported_headers",
116+
out = "includes",
117+
cmd = (
118+
"mkdir -p $OUT && " +
119+
"cat `dirname $(location :shared)`/includes/*.h > $OUT/_cgo_export.h"
120+
),
121+
)
122+
123+
prebuilt_cxx_library(
124+
name = "cxx_so_with_header",
125+
header_dirs = [":cgo_exported_headers"],
126+
shared_lib = ":shared",
127+
)
128+
</pre>{/literal}
129+
{/param}
130+
131+
{/call} // close buck.rule
132+
133+
{/param}
134+
{/call}
135+
{/template}

src/com/facebook/buck/features/go/GoBinary.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class GoBinary extends AbstractBuildRuleWithDeclaredAndExtraDeps
5959
@AddToRuleKey private final Linker cxxLinker;
6060
@AddToRuleKey private final ImmutableList<Arg> linkerArgs;
6161
@AddToRuleKey private final ImmutableList<Arg> cxxLinkerArgs;
62+
@AddToRuleKey private final GoLinkStep.BuildMode buildMode;
6263
@AddToRuleKey private final GoLinkStep.LinkMode linkMode;
6364
@AddToRuleKey private final GoPlatform platform;
6465

@@ -77,6 +78,7 @@ public GoBinary(
7778
GoCompile mainObject,
7879
Tool linker,
7980
Linker cxxLinker,
81+
GoLinkStep.BuildMode buildMode,
8082
GoLinkStep.LinkMode linkMode,
8183
ImmutableList<Arg> linkerArgs,
8284
ImmutableList<Arg> cxxLinkerArgs,
@@ -93,15 +95,13 @@ public GoBinary(
9395
this.platform = platform;
9496
this.withDownwardApi = withDownwardApi;
9597

96-
String outputFormat = "%s/" + buildTarget.getShortName();
97-
if (platform.getGoOs() == GoOs.WINDOWS) {
98-
outputFormat = outputFormat + ".exe";
99-
}
98+
String outputFormat = getOutputFormat(buildTarget, platform, buildMode);
10099
this.output =
101100
BuildTargetPaths.getGenPath(projectFilesystem.getBuckPaths(), buildTarget, outputFormat);
102101

103102
this.linkerArgs = linkerArgs;
104103
this.linkMode = linkMode;
104+
this.buildMode = buildMode;
105105
}
106106

107107
@Override
@@ -124,12 +124,37 @@ protected RelPath getPathToBinaryDirectory() {
124124
return output.getParent();
125125
}
126126

127+
private String getOutputFormat(
128+
BuildTarget buildTarget, GoPlatform platform, GoLinkStep.BuildMode buildMode) {
129+
String outputFormat = "%s/" + buildTarget.getShortName();
130+
131+
switch (buildMode) {
132+
case C_SHARED:
133+
if (platform.getGoOs() == GoOs.DARWIN) {
134+
return outputFormat + ".dylib";
135+
}
136+
if (platform.getGoOs() == GoOs.WINDOWS) {
137+
return outputFormat + ".dll";
138+
}
139+
return outputFormat + ".so";
140+
case C_ARCHIVE:
141+
return outputFormat + ".a";
142+
case EXECUTABLE:
143+
if (platform.getGoOs() == GoOs.WINDOWS) {
144+
return outputFormat + ".exe";
145+
}
146+
}
147+
148+
return outputFormat;
149+
}
150+
127151
@Override
128152
public ImmutableList<Step> getBuildSteps(
129153
BuildContext context, BuildableContext buildableContext) {
130154

131155
buildableContext.recordArtifact(output.getPath());
132156

157+
ProjectFilesystem filesystem = getProjectFilesystem();
133158
SourcePathResolverAdapter resolver = context.getSourcePathResolver();
134159
ImmutableList.Builder<Step> steps = ImmutableList.builder();
135160

@@ -151,9 +176,7 @@ public ImmutableList<Step> getBuildSteps(
151176
steps.add(
152177
MkdirStep.of(
153178
BuildCellRelativePath.fromCellRelativePath(
154-
context.getBuildCellRootPath(),
155-
getProjectFilesystem(),
156-
outputResourcePath.getParent())));
179+
context.getBuildCellRootPath(), filesystem, outputResourcePath.getParent())));
157180
steps.add(
158181
CopyStep.forDirectory(
159182
resolver.getCellUnsafeRelPath(resource),
@@ -163,9 +186,7 @@ public ImmutableList<Step> getBuildSteps(
163186
steps.add(
164187
MkdirStep.of(
165188
BuildCellRelativePath.fromCellRelativePath(
166-
context.getBuildCellRootPath(),
167-
getProjectFilesystem(),
168-
outputResourcePath.getParent())));
189+
context.getBuildCellRootPath(), filesystem, outputResourcePath.getParent())));
169190
steps.add(CopyStep.forFile(resolver.getCellUnsafeRelPath(resource), outputResourcePath));
170191
}
171192
}
@@ -212,7 +233,7 @@ public ImmutableList<Step> getBuildSteps(
212233
ImmutableList.of(linkTree.getRoot()),
213234
platform,
214235
resolver.getCellUnsafeRelPath(mainObject.getSourcePathToOutput()).getPath(),
215-
GoLinkStep.BuildMode.EXECUTABLE,
236+
buildMode,
216237
linkMode,
217238
output.getPath(),
218239
ProjectFilesystemUtils.relativize(

src/com/facebook/buck/features/go/GoBinaryDescription.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public BuildRule createBuildRule(
116116
goBuckConfig,
117117
downwardApiConfig,
118118
args.getLinkStyle().orElse(Linker.LinkableDepType.STATIC_PIC),
119+
GoLinkStep.BuildMode.EXECUTABLE,
119120
args.getLinkMode(),
120121
args.getSrcs(),
121122
args.getResources(),

src/com/facebook/buck/features/go/GoDescriptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Collection<Description<?>> getDescriptions(DescriptionCreationContext con
4141

4242
return Arrays.asList(
4343
new GoBinaryDescription(goBuckConfig, downwardApiConfig, toolchainProvider),
44+
new GoExportedLibraryDescription(goBuckConfig, downwardApiConfig, toolchainProvider),
4445
new GoLibraryDescription(goBuckConfig, downwardApiConfig, toolchainProvider),
4546
new CgoLibraryDescription(
4647
goBuckConfig, cxxBuckConfig, downwardApiConfig, toolchainProvider),

src/com/facebook/buck/features/go/GoDescriptors.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ static GoBinary createGoBinaryRule(
321321
GoBuckConfig goBuckConfig,
322322
DownwardApiConfig downwardApiConfig,
323323
Linker.LinkableDepType linkStyle,
324+
GoLinkStep.BuildMode buildMode,
324325
Optional<GoLinkStep.LinkMode> linkMode,
325326
ImmutableSet<SourcePath> srcs,
326327
ImmutableSortedSet<SourcePath> resources,
@@ -429,7 +430,9 @@ static GoBinary createGoBinaryRule(
429430
if (!linkMode.isPresent()) {
430431
linkMode =
431432
Optional.of(
432-
(cxxLinkerArgs.size() > 0)
433+
(cxxLinkerArgs.size() > 0
434+
|| buildMode == GoLinkStep.BuildMode.C_SHARED
435+
|| buildMode == GoLinkStep.BuildMode.C_ARCHIVE)
433436
? GoLinkStep.LinkMode.EXTERNAL
434437
: GoLinkStep.LinkMode.INTERNAL);
435438
}
@@ -455,6 +458,7 @@ static GoBinary createGoBinaryRule(
455458
library,
456459
platform.getLinker(),
457460
cxxLinker,
461+
buildMode,
458462
linkMode.get(),
459463
ImmutableList.copyOf(linkerArgs),
460464
cxxLinkerArgs,
@@ -508,6 +512,7 @@ static Tool getTestMainGenerator(
508512
goBuckConfig,
509513
downwardApiConfig,
510514
Linker.LinkableDepType.STATIC_PIC,
515+
GoLinkStep.BuildMode.EXECUTABLE,
511516
Optional.empty(),
512517
ImmutableSet.of(writeFile.getSourcePathToOutput()),
513518
ImmutableSortedSet.of(),

0 commit comments

Comments
 (0)