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

Commit 43ac012

Browse files
yottatsafacebook-github-bot
authored andcommitted
support c_shared mode
Summary: Revival of D16782936: 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: fa0a4f7e3af8c2852e03bc827fc15901c6079246
1 parent 92fcfca commit 43ac012

18 files changed

Lines changed: 527 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/CGoLibrary.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,25 @@
7979
*/
8080
public class CGoLibrary extends NoopBuildRuleWithDeclaredAndExtraDeps {
8181
private final ImmutableList<SourcePath> goFiles;
82+
private final Path packageName;
8283
private final SourcePath output;
84+
private final SourcePath exportHeader;
8385
private final Iterable<BuildRule> linkableDeps;
8486

8587
private CGoLibrary(
8688
BuildRuleParams params,
8789
BuildTarget buildTarget,
8890
ProjectFilesystem projectFilesystem,
8991
ImmutableList<SourcePath> goFiles,
92+
Path packageName,
93+
SourcePath exportHeader,
9094
SourcePath output,
9195
Iterable<BuildRule> linkableDeps) {
9296
super(buildTarget, projectFilesystem, params);
9397

9498
this.goFiles = goFiles;
99+
this.packageName = packageName;
100+
this.exportHeader = exportHeader;
95101
this.output = output;
96102
this.linkableDeps = linkableDeps;
97103
}
@@ -102,6 +108,7 @@ public static BuildRule create(
102108
ProjectFilesystem projectFilesystem,
103109
ActionGraphBuilder graphBuilder,
104110
CellPathResolver cellRoots,
111+
Path packageName,
105112
CxxBuckConfig cxxBuckConfig,
106113
DownwardApiConfig downwardApiConfig,
107114
GoPlatform platform,
@@ -328,6 +335,8 @@ public static BuildRule create(
328335
.addAll(genSource.getGoFiles())
329336
.add(Objects.requireNonNull(cgoImport.getSourcePathToOutput()))
330337
.build(),
338+
packageName,
339+
Objects.requireNonNull(genSource.getExportHeader()),
331340
Objects.requireNonNull(cgoAllBin.getSourcePathToOutput()),
332341
Objects.requireNonNull(allDeps.get(graphBuilder, platform.getCxxPlatform()))));
333342
}
@@ -445,11 +454,21 @@ public ImmutableList<SourcePath> getGeneratedGoSource() {
445454
return goFiles;
446455
}
447456

457+
/** returns package name */
458+
public Path getPackageName() {
459+
return packageName;
460+
}
461+
448462
/** returns compiled linkable file source path */
449463
public SourcePath getOutput() {
450464
return output;
451465
}
452466

467+
/** returns _export.h file generated by gen-source step * */
468+
public SourcePath getExportHeader() {
469+
return exportHeader;
470+
}
471+
453472
public Iterable<BuildRule> getLinkableDeps() {
454473
return linkableDeps;
455474
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.google.common.collect.ImmutableSet;
5252
import com.google.common.collect.ImmutableSortedSet;
5353
import com.google.common.collect.Iterables;
54+
import java.nio.file.Path;
5455
import java.nio.file.Paths;
5556
import java.util.Arrays;
5657
import java.util.Optional;
@@ -174,6 +175,11 @@ public BuildRule createBuildRule(
174175
.map(BuildRule::getBuildTarget)
175176
.collect(ImmutableList.toImmutableList());
176177

178+
Path packageName =
179+
args.getPackageName()
180+
.map(Paths::get)
181+
.orElse(goBuckConfig.getDefaultPackageName(buildTarget));
182+
177183
BuildTarget cgoLibTarget = buildTarget.withAppendedFlavors(InternalFlavor.of("cgo"));
178184
CGoLibrary lib =
179185
(CGoLibrary)
@@ -183,6 +189,7 @@ public BuildRule createBuildRule(
183189
projectFilesystem,
184190
graphBuilder,
185191
context.getCellPathResolver(),
192+
packageName,
186193
cxxBuckConfig,
187194
downwardApiConfig,
188195
platform.get(),

0 commit comments

Comments
 (0)