Skip to content

Commit

Permalink
refactor(buildtool): use list for OpenSSL ./Configure flags (ooni#1367)
Browse files Browse the repository at this point in the history
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
ooni#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`.
  • Loading branch information
bassosimone authored and Murphy-OrangeMud committed Feb 13, 2024
1 parent 736e5e6 commit 60f5ca1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 69 deletions.
50 changes: 27 additions & 23 deletions internal/cmd/buildtool/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ func androidSubcommand() *cobra.Command {
Use: "android",
Short: "Builds ooniprobe, miniooni, and oonimkall for android",
}

cmd.AddCommand(&cobra.Command{
Use: "gomobile",
Short: "Builds oonimkall for android using gomobile",
Run: func(cmd *cobra.Command, args []string) {
androidBuildGomobile(&buildDeps{})
},
})

cmd.AddCommand(&cobra.Command{
Use: "cli",
Short: "Builds ooniprobe and miniooni for usage within termux",
Run: func(cmd *cobra.Command, args []string) {
androidBuildCLIAll(&buildDeps{})
},
})

cmd.AddCommand(&cobra.Command{
Use: "cdeps {zlib|openssl|libevent|tor} [zlib|openssl|libevent|tor...]",
Short: "Cross compiles C dependencies for Android",
Expand All @@ -50,6 +53,7 @@ func androidSubcommand() *cobra.Command {
},
Args: cobra.MinimumNArgs(1),
})

return cmd
}

Expand Down Expand Up @@ -161,7 +165,7 @@ func androidBuildCLIProductArch(
androidHome string,
ndkDir string,
) {
cgo := newAndroidCBuildEnv(androidHome, ndkDir, ooniArch)
cgo := androidNewCBuildEnv(androidHome, ndkDir, ooniArch)

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

Expand Down Expand Up @@ -203,33 +207,33 @@ func androidBuildCLIProductArch(
runtimex.Try0(shellx.RunEx(defaultShellxConfig(), argv, envp))
}

// newAndroidCBuildEnv creates a new [cBuildEnv] for the
// androidNewCBuildEnv creates a new [cBuildEnv] for the
// given ooniArch ("arm", "arm64", "386", "amd64").
func newAndroidCBuildEnv(androidHome, ndkDir, ooniArch string) *cBuildEnv {
func androidNewCBuildEnv(androidHome, ndkDir, ooniArch string) *cBuildEnv {
binpath := androidNDKBinPath(ndkDir)
destdir := runtimex.Try1(filepath.Abs(filepath.Join( // must be absolute
"internal", "libtor", "android", ooniArch,
)))
out := &cBuildEnv{
ANDROID_HOME: androidHome,
ANDROID_NDK_ROOT: ndkDir,
AS: "", // later
AR: filepath.Join(binpath, "llvm-ar"),
BINPATH: binpath,
CC: "", // later
CFLAGS: androidCflags(ooniArch),
CONFIGURE_HOST: "", // later
DESTDIR: destdir,
CXX: "", // later
CXXFLAGS: androidCflags(ooniArch),
GOARCH: ooniArch,
GOARM: "", // maybe later
LD: filepath.Join(binpath, "ld"),
LDFLAGS: []string{}, // empty
OPENSSL_API_DEFINE: "-D__ANDROID_API__=21",
OPENSSL_COMPILER: "", // later
RANLIB: filepath.Join(binpath, "llvm-ranlib"),
STRIP: filepath.Join(binpath, "llvm-strip"),
ANDROID_HOME: androidHome,
ANDROID_NDK_ROOT: ndkDir,
AS: "", // later
AR: filepath.Join(binpath, "llvm-ar"),
BINPATH: binpath,
CC: "", // later
CFLAGS: androidCflags(ooniArch),
CONFIGURE_HOST: "", // later
DESTDIR: destdir,
CXX: "", // later
CXXFLAGS: androidCflags(ooniArch),
GOARCH: ooniArch,
GOARM: "", // maybe later
LD: filepath.Join(binpath, "ld"),
LDFLAGS: []string{}, // empty
OPENSSL_COMPILER: "", // later
OPENSSL_POST_COMPILER_FLAGS: []string{"-D__ANDROID_API__=21"},
RANLIB: filepath.Join(binpath, "llvm-ranlib"),
STRIP: filepath.Join(binpath, "llvm-strip"),
}
switch ooniArch {
case "arm":
Expand Down Expand Up @@ -395,7 +399,7 @@ func androidCdepsBuildArch(
ndkDir string,
name string,
) {
cdenv := newAndroidCBuildEnv(androidHome, ndkDir, arch)
cdenv := androidNewCBuildEnv(androidHome, ndkDir, arch)
switch name {
case "libevent":
cdepsLibeventBuildMain(cdenv, deps)
Expand Down
45 changes: 23 additions & 22 deletions internal/cmd/buildtool/cbuildenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type cBuildEnv struct {
// LDFLAGS contains the LDFLAGS to use when compiling.
LDFLAGS []string

// OPENSSL_API_DEFINE is an extra define we need to add on Android.
OPENSSL_API_DEFINE string

// OPENSSL_COMPILER is the compiler name for OpenSSL.
OPENSSL_COMPILER string

// OPENSSL_POST_COMPILER_FLAGS contains extra flags to pass after OPENSSL_COMPILER
OPENSSL_POST_COMPILER_FLAGS []string

// RANLIB is the path to the ranlib tool.
RANLIB string

Expand All @@ -86,29 +86,30 @@ type cBuildEnv struct {
// environment variables to CFLAGS, CXXFLAGS, etc.
func cBuildMerge(global, local *cBuildEnv) *cBuildEnv {
out := &cBuildEnv{
ANDROID_HOME: global.ANDROID_HOME,
ANDROID_NDK_ROOT: global.ANDROID_NDK_ROOT,
AR: global.AR,
AS: global.AS,
BINPATH: global.BINPATH,
CC: global.CC,
CFLAGS: append([]string{}, global.CFLAGS...),
CONFIGURE_HOST: global.CONFIGURE_HOST,
DESTDIR: global.DESTDIR,
CXX: global.CXX,
CXXFLAGS: append([]string{}, global.CXXFLAGS...),
GOARCH: global.GOARCH,
GOARM: global.GOARM,
LD: global.LD,
LDFLAGS: append([]string{}, global.LDFLAGS...),
OPENSSL_API_DEFINE: global.OPENSSL_API_DEFINE,
OPENSSL_COMPILER: global.OPENSSL_COMPILER,
RANLIB: global.RANLIB,
STRIP: global.STRIP,
ANDROID_HOME: global.ANDROID_HOME,
ANDROID_NDK_ROOT: global.ANDROID_NDK_ROOT,
AR: global.AR,
AS: global.AS,
BINPATH: global.BINPATH,
CC: global.CC,
CFLAGS: append([]string{}, global.CFLAGS...),
CONFIGURE_HOST: global.CONFIGURE_HOST,
DESTDIR: global.DESTDIR,
CXX: global.CXX,
CXXFLAGS: append([]string{}, global.CXXFLAGS...),
GOARCH: global.GOARCH,
GOARM: global.GOARM,
LD: global.LD,
LDFLAGS: append([]string{}, global.LDFLAGS...),
OPENSSL_COMPILER: global.OPENSSL_COMPILER,
OPENSSL_POST_COMPILER_FLAGS: append([]string{}, global.OPENSSL_POST_COMPILER_FLAGS...),
RANLIB: global.RANLIB,
STRIP: global.STRIP,
}
out.CFLAGS = append(out.CFLAGS, local.CFLAGS...)
out.CXXFLAGS = append(out.CXXFLAGS, local.CXXFLAGS...)
out.LDFLAGS = append(out.LDFLAGS, local.LDFLAGS...)
out.OPENSSL_POST_COMPILER_FLAGS = append(out.OPENSSL_POST_COMPILER_FLAGS, local.OPENSSL_POST_COMPILER_FLAGS...)
return out
}

Expand Down
4 changes: 1 addition & 3 deletions internal/cmd/buildtool/cdepsopenssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ func cdepsOpenSSLBuildMain(globalEnv *cBuildEnv, deps buildtoolmodel.Dependencie
"no-rc2", "no-rc4", "no-rc5", "no-rmd160", "no-whirlpool", "no-dso",
"no-ui-console", "no-shared", "no-unit-test", globalEnv.OPENSSL_COMPILER,
))
if globalEnv.OPENSSL_API_DEFINE != "" {
argv.Append(globalEnv.OPENSSL_API_DEFINE)
}
argv.Append(globalEnv.OPENSSL_POST_COMPILER_FLAGS...)
argv.Append("--libdir=lib", "--prefix=/", "--openssldir=/")
runtimex.Try0(shellx.RunEx(defaultShellxConfig(), argv, envp))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type Dependencies interface {
// function returns the Android home path.
AndroidSDKCheck() string

// GOOS returns the current GOOS.
GOOS() string

// GOPATH returns the current GOPATH.
GOPATH() string

Expand Down Expand Up @@ -49,7 +52,4 @@ type Dependencies interface {
// WindowsMingwCheck makes sure we're using the
// expected version of mingw-w64.
WindowsMingwCheck()

// GOOS returns the current GOOS.
GOOS() string
}
36 changes: 18 additions & 18 deletions internal/cmd/buildtool/linuxcdeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ func linuxCdepsBuildMain(name string, deps buildtoolmodel.Dependencies) {
"internal", "libtor", "linux", runtime.GOARCH,
)))
globalEnv := &cBuildEnv{
ANDROID_HOME: "",
ANDROID_NDK_ROOT: "",
AR: "",
BINPATH: "",
CC: "",
CFLAGS: cflags,
CONFIGURE_HOST: "",
DESTDIR: destdir,
CXX: "",
CXXFLAGS: cflags,
GOARCH: "",
GOARM: "",
LD: "",
LDFLAGS: []string{},
OPENSSL_API_DEFINE: "",
OPENSSL_COMPILER: "linux-x86_64",
RANLIB: "",
STRIP: "",
ANDROID_HOME: "",
ANDROID_NDK_ROOT: "",
AR: "",
BINPATH: "",
CC: "",
CFLAGS: cflags,
CONFIGURE_HOST: "",
DESTDIR: destdir,
CXX: "",
CXXFLAGS: cflags,
GOARCH: "",
GOARM: "",
LD: "",
LDFLAGS: []string{},
OPENSSL_COMPILER: "linux-x86_64",
OPENSSL_POST_COMPILER_FLAGS: []string{},
RANLIB: "",
STRIP: "",
}
switch name {
case "libevent":
Expand Down

0 comments on commit 60f5ca1

Please sign in to comment.