Skip to content

Commit 06cda7b

Browse files
committed
[backport] refactor(buildtool): use list for OpenSSL ./Configure flags
This diff backports #1367 to the release/3.19 branch. We currently have a string variable in `cBuildEnv` named `OPENSSL_API_DEFINE` that we append to OpenSSL's `./Configure` invocation to force using the proper Android API. However, for building for iOS (a need documented by ooni/probe#2564), we need a list of strings, because there is more than a single scalar that we need to append to the `./Configure` invocation (as shown by the MVP implementation at #1366). Hence, this diff, which introduces a string list named `OPENSSL_POST_COMPILER_FLAGS` that contains strings to append to the `./Configure` command line _after_ the OS/compiler flag. We specifically named the variable "post compiler" because there is another variable in the same `cBuildEnv` struct called `OPENSSL_COMPILER`.
1 parent 05e3716 commit 06cda7b

File tree

5 files changed

+72
-69
lines changed

5 files changed

+72
-69
lines changed

internal/cmd/buildtool/android.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@ func androidSubcommand() *cobra.Command {
2626
Use: "android",
2727
Short: "Builds ooniprobe, miniooni, and oonimkall for android",
2828
}
29+
2930
cmd.AddCommand(&cobra.Command{
3031
Use: "gomobile",
3132
Short: "Builds oonimkall for android using gomobile",
3233
Run: func(cmd *cobra.Command, args []string) {
3334
androidBuildGomobile(&buildDeps{})
3435
},
3536
})
37+
3638
cmd.AddCommand(&cobra.Command{
3739
Use: "cli",
3840
Short: "Builds ooniprobe and miniooni for usage within termux",
3941
Run: func(cmd *cobra.Command, args []string) {
4042
androidBuildCLIAll(&buildDeps{})
4143
},
4244
})
45+
4346
cmd.AddCommand(&cobra.Command{
4447
Use: "cdeps {zlib|openssl|libevent|tor} [zlib|openssl|libevent|tor...]",
4548
Short: "Cross compiles C dependencies for Android",
@@ -50,6 +53,7 @@ func androidSubcommand() *cobra.Command {
5053
},
5154
Args: cobra.MinimumNArgs(1),
5255
})
56+
5357
return cmd
5458
}
5559

@@ -161,7 +165,7 @@ func androidBuildCLIProductArch(
161165
androidHome string,
162166
ndkDir string,
163167
) {
164-
cgo := newAndroidCBuildEnv(androidHome, ndkDir, ooniArch)
168+
cgo := androidNewCBuildEnv(androidHome, ndkDir, ooniArch)
165169

166170
log.Infof("building %s for android/%s", product.Pkg, ooniArch)
167171

@@ -203,33 +207,33 @@ func androidBuildCLIProductArch(
203207
runtimex.Try0(shellx.RunEx(defaultShellxConfig(), argv, envp))
204208
}
205209

206-
// newAndroidCBuildEnv creates a new [cBuildEnv] for the
210+
// androidNewCBuildEnv creates a new [cBuildEnv] for the
207211
// given ooniArch ("arm", "arm64", "386", "amd64").
208-
func newAndroidCBuildEnv(androidHome, ndkDir, ooniArch string) *cBuildEnv {
212+
func androidNewCBuildEnv(androidHome, ndkDir, ooniArch string) *cBuildEnv {
209213
binpath := androidNDKBinPath(ndkDir)
210214
destdir := runtimex.Try1(filepath.Abs(filepath.Join( // must be absolute
211215
"internal", "libtor", "android", ooniArch,
212216
)))
213217
out := &cBuildEnv{
214-
ANDROID_HOME: androidHome,
215-
ANDROID_NDK_ROOT: ndkDir,
216-
AS: "", // later
217-
AR: filepath.Join(binpath, "llvm-ar"),
218-
BINPATH: binpath,
219-
CC: "", // later
220-
CFLAGS: androidCflags(ooniArch),
221-
CONFIGURE_HOST: "", // later
222-
DESTDIR: destdir,
223-
CXX: "", // later
224-
CXXFLAGS: androidCflags(ooniArch),
225-
GOARCH: ooniArch,
226-
GOARM: "", // maybe later
227-
LD: filepath.Join(binpath, "ld"),
228-
LDFLAGS: []string{}, // empty
229-
OPENSSL_API_DEFINE: "-D__ANDROID_API__=21",
230-
OPENSSL_COMPILER: "", // later
231-
RANLIB: filepath.Join(binpath, "llvm-ranlib"),
232-
STRIP: filepath.Join(binpath, "llvm-strip"),
218+
ANDROID_HOME: androidHome,
219+
ANDROID_NDK_ROOT: ndkDir,
220+
AS: "", // later
221+
AR: filepath.Join(binpath, "llvm-ar"),
222+
BINPATH: binpath,
223+
CC: "", // later
224+
CFLAGS: androidCflags(ooniArch),
225+
CONFIGURE_HOST: "", // later
226+
DESTDIR: destdir,
227+
CXX: "", // later
228+
CXXFLAGS: androidCflags(ooniArch),
229+
GOARCH: ooniArch,
230+
GOARM: "", // maybe later
231+
LD: filepath.Join(binpath, "ld"),
232+
LDFLAGS: []string{}, // empty
233+
OPENSSL_COMPILER: "", // later
234+
OPENSSL_POST_COMPILER_FLAGS: []string{"-D__ANDROID_API__=21"},
235+
RANLIB: filepath.Join(binpath, "llvm-ranlib"),
236+
STRIP: filepath.Join(binpath, "llvm-strip"),
233237
}
234238
switch ooniArch {
235239
case "arm":
@@ -395,7 +399,7 @@ func androidCdepsBuildArch(
395399
ndkDir string,
396400
name string,
397401
) {
398-
cdenv := newAndroidCBuildEnv(androidHome, ndkDir, arch)
402+
cdenv := androidNewCBuildEnv(androidHome, ndkDir, arch)
399403
switch name {
400404
case "libevent":
401405
cdepsLibeventBuildMain(cdenv, deps)

internal/cmd/buildtool/cbuildenv.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ type cBuildEnv struct {
6161
// LDFLAGS contains the LDFLAGS to use when compiling.
6262
LDFLAGS []string
6363

64-
// OPENSSL_API_DEFINE is an extra define we need to add on Android.
65-
OPENSSL_API_DEFINE string
66-
6764
// OPENSSL_COMPILER is the compiler name for OpenSSL.
6865
OPENSSL_COMPILER string
6966

67+
// OPENSSL_POST_COMPILER_FLAGS contains extra flags to pass after OPENSSL_COMPILER
68+
OPENSSL_POST_COMPILER_FLAGS []string
69+
7070
// RANLIB is the path to the ranlib tool.
7171
RANLIB string
7272

@@ -86,29 +86,30 @@ type cBuildEnv struct {
8686
// environment variables to CFLAGS, CXXFLAGS, etc.
8787
func cBuildMerge(global, local *cBuildEnv) *cBuildEnv {
8888
out := &cBuildEnv{
89-
ANDROID_HOME: global.ANDROID_HOME,
90-
ANDROID_NDK_ROOT: global.ANDROID_NDK_ROOT,
91-
AR: global.AR,
92-
AS: global.AS,
93-
BINPATH: global.BINPATH,
94-
CC: global.CC,
95-
CFLAGS: append([]string{}, global.CFLAGS...),
96-
CONFIGURE_HOST: global.CONFIGURE_HOST,
97-
DESTDIR: global.DESTDIR,
98-
CXX: global.CXX,
99-
CXXFLAGS: append([]string{}, global.CXXFLAGS...),
100-
GOARCH: global.GOARCH,
101-
GOARM: global.GOARM,
102-
LD: global.LD,
103-
LDFLAGS: append([]string{}, global.LDFLAGS...),
104-
OPENSSL_API_DEFINE: global.OPENSSL_API_DEFINE,
105-
OPENSSL_COMPILER: global.OPENSSL_COMPILER,
106-
RANLIB: global.RANLIB,
107-
STRIP: global.STRIP,
89+
ANDROID_HOME: global.ANDROID_HOME,
90+
ANDROID_NDK_ROOT: global.ANDROID_NDK_ROOT,
91+
AR: global.AR,
92+
AS: global.AS,
93+
BINPATH: global.BINPATH,
94+
CC: global.CC,
95+
CFLAGS: append([]string{}, global.CFLAGS...),
96+
CONFIGURE_HOST: global.CONFIGURE_HOST,
97+
DESTDIR: global.DESTDIR,
98+
CXX: global.CXX,
99+
CXXFLAGS: append([]string{}, global.CXXFLAGS...),
100+
GOARCH: global.GOARCH,
101+
GOARM: global.GOARM,
102+
LD: global.LD,
103+
LDFLAGS: append([]string{}, global.LDFLAGS...),
104+
OPENSSL_COMPILER: global.OPENSSL_COMPILER,
105+
OPENSSL_POST_COMPILER_FLAGS: append([]string{}, global.OPENSSL_POST_COMPILER_FLAGS...),
106+
RANLIB: global.RANLIB,
107+
STRIP: global.STRIP,
108108
}
109109
out.CFLAGS = append(out.CFLAGS, local.CFLAGS...)
110110
out.CXXFLAGS = append(out.CXXFLAGS, local.CXXFLAGS...)
111111
out.LDFLAGS = append(out.LDFLAGS, local.LDFLAGS...)
112+
out.OPENSSL_POST_COMPILER_FLAGS = append(out.OPENSSL_POST_COMPILER_FLAGS, local.OPENSSL_POST_COMPILER_FLAGS...)
112113
return out
113114
}
114115

internal/cmd/buildtool/cdepsopenssl.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ func cdepsOpenSSLBuildMain(globalEnv *cBuildEnv, deps buildtoolmodel.Dependencie
6767
"no-rc2", "no-rc4", "no-rc5", "no-rmd160", "no-whirlpool", "no-dso",
6868
"no-ui-console", "no-shared", "no-unit-test", globalEnv.OPENSSL_COMPILER,
6969
))
70-
if globalEnv.OPENSSL_API_DEFINE != "" {
71-
argv.Append(globalEnv.OPENSSL_API_DEFINE)
72-
}
70+
argv.Append(globalEnv.OPENSSL_POST_COMPILER_FLAGS...)
7371
argv.Append("--libdir=lib", "--prefix=/", "--openssldir=/")
7472
runtimex.Try0(shellx.RunEx(defaultShellxConfig(), argv, envp))
7573

internal/cmd/buildtool/internal/buildtoolmodel/buildtoolmodel.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type Dependencies interface {
1818
// function returns the Android home path.
1919
AndroidSDKCheck() string
2020

21+
// GOOS returns the current GOOS.
22+
GOOS() string
23+
2124
// GOPATH returns the current GOPATH.
2225
GOPATH() string
2326

@@ -49,7 +52,4 @@ type Dependencies interface {
4952
// WindowsMingwCheck makes sure we're using the
5053
// expected version of mingw-w64.
5154
WindowsMingwCheck()
52-
53-
// GOOS returns the current GOOS.
54-
GOOS() string
5555
}

internal/cmd/buildtool/linuxcdeps.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ func linuxCdepsBuildMain(name string, deps buildtoolmodel.Dependencies) {
5050
"internal", "libtor", "linux", runtime.GOARCH,
5151
)))
5252
globalEnv := &cBuildEnv{
53-
ANDROID_HOME: "",
54-
ANDROID_NDK_ROOT: "",
55-
AR: "",
56-
BINPATH: "",
57-
CC: "",
58-
CFLAGS: cflags,
59-
CONFIGURE_HOST: "",
60-
DESTDIR: destdir,
61-
CXX: "",
62-
CXXFLAGS: cflags,
63-
GOARCH: "",
64-
GOARM: "",
65-
LD: "",
66-
LDFLAGS: []string{},
67-
OPENSSL_API_DEFINE: "",
68-
OPENSSL_COMPILER: "linux-x86_64",
69-
RANLIB: "",
70-
STRIP: "",
53+
ANDROID_HOME: "",
54+
ANDROID_NDK_ROOT: "",
55+
AR: "",
56+
BINPATH: "",
57+
CC: "",
58+
CFLAGS: cflags,
59+
CONFIGURE_HOST: "",
60+
DESTDIR: destdir,
61+
CXX: "",
62+
CXXFLAGS: cflags,
63+
GOARCH: "",
64+
GOARM: "",
65+
LD: "",
66+
LDFLAGS: []string{},
67+
OPENSSL_COMPILER: "linux-x86_64",
68+
OPENSSL_POST_COMPILER_FLAGS: []string{},
69+
RANLIB: "",
70+
STRIP: "",
7171
}
7272
switch name {
7373
case "libevent":

0 commit comments

Comments
 (0)