1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use super::tempfile::TempFile;
use super::util::{self, BuildTarget};
use crate::config::AndroidBuildTarget;
use crate::config::AndroidConfig;
use cargo::core::compiler::Executor;
use cargo::core::compiler::{CompileKind, CompileMode, CompileTarget};
use cargo::core::manifest::TargetSourcePath;
use cargo::core::{PackageId, Target, TargetKind, Workspace};
use cargo::ops::CompileOptions;
use cargo::util::{process, CargoResult, ProcessBuilder};
use failure::format_err;
use multimap::MultiMap;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

pub struct SharedLibrary {
    pub abi: AndroidBuildTarget,
    pub path: PathBuf,
    pub filename: String,
}

pub struct SharedLibraries {
    pub shared_libraries: MultiMap<BuildTarget, SharedLibrary>,
}

/// For each build target and cargo binary or example target, produce a shared library
pub fn build_shared_libraries(
    workspace: &Workspace,
    config: &AndroidConfig,
    mut options: CompileOptions,
    root_build_dir: &PathBuf,
) -> CargoResult<SharedLibraries> {
    let android_native_glue_src_path = write_native_app_glue_src(&root_build_dir)?;

    let shared_libraries: Arc<Mutex<MultiMap<BuildTarget, SharedLibrary>>> =
        Arc::new(Mutex::new(MultiMap::new()));
    for &build_target in config.build_targets.iter() {
        // Directory that will contain files specific to this build target
        let build_target_dir = root_build_dir.join(build_target.android_abi());
        fs::create_dir_all(&build_target_dir).unwrap();

        // Set environment variables needed for use with the cc crate
        std::env::set_var("CC", util::find_clang(config, build_target)?);
        std::env::set_var("CXX", util::find_clang_cpp(config, build_target)?);
        std::env::set_var("AR", util::find_ar(config, build_target)?);

        // Use libc++. It is current default C++ runtime
        std::env::set_var("CXXSTDLIB", "c++");

        // Generate cmake toolchain and set environment variables to allow projects which use the cmake crate to build correctly
        let cmake_toolchain_path = write_cmake_toolchain(config, &build_target_dir, build_target)?;
        std::env::set_var("CMAKE_TOOLCHAIN_FILE", cmake_toolchain_path);
        std::env::set_var("CMAKE_GENERATOR", r#"Unix Makefiles"#);
        std::env::set_var("CMAKE_MAKE_PROGRAM", util::make_path(config));

        // Build android_native_glue
        let android_native_glue_object = build_android_native_glue(
            config,
            &android_native_glue_src_path,
            &build_target_dir,
            build_target,
        )?;

        // Configure compilation options so that we will build the desired build_target
        options.build_config.requested_kind =
            CompileKind::Target(CompileTarget::new(build_target.rust_triple())?);

        // Create executor
        let config = Arc::new(config.clone());
        let executor: Arc<dyn Executor> = Arc::new(SharedLibraryExecutor {
            config: Arc::clone(&config),
            build_target_dir: build_target_dir.clone(),
            android_native_glue_object,
            build_target,
            shared_libraries: shared_libraries.clone(),
        });

        // Compile all targets for the requested build target
        cargo::ops::compile_with_exec(workspace, &options, &executor)?;
    }

    // Remove the set of targets from the reference counted mutex
    let mut shared_libraries = shared_libraries.lock().unwrap();
    let shared_libraries = std::mem::replace(&mut *shared_libraries, MultiMap::new());

    Ok(SharedLibraries { shared_libraries })
}

/// Executor which builds binary and example targets as static libraries
struct SharedLibraryExecutor {
    config: Arc<AndroidConfig>,
    build_target_dir: PathBuf,
    android_native_glue_object: PathBuf,
    build_target: AndroidBuildTarget,

    // Shared libraries built by the executor are added to this multimap
    shared_libraries: Arc<Mutex<MultiMap<BuildTarget, SharedLibrary>>>,
}

impl<'a> Executor for SharedLibraryExecutor {
    fn exec(
        &self,
        cmd: ProcessBuilder,
        _id: PackageId,
        target: &Target,
        mode: CompileMode,
        on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
        on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
    ) -> CargoResult<()> {
        if mode == CompileMode::Build
            && (target.kind() == &TargetKind::Bin || target.kind() == &TargetKind::ExampleBin)
        {
            let mut new_args = cmd.get_args().to_owned();

            //
            // Determine source path
            //
            let path = if let TargetSourcePath::Path(path) = target.src_path() {
                path.to_owned()
            } else {
                // Ignore other values
                return Ok(());
            };

            let original_src_filepath = path.canonicalize()?;

            //
            // Generate source file that will be built
            //
            // Determine the name of the temporary file
            let tmp_lib_filepath = original_src_filepath.parent().unwrap().join(format!(
                "__cargo_apk_{}.tmp",
                original_src_filepath
                    .file_stem()
                    .map(|s| s.to_string_lossy().into_owned())
                    .unwrap_or_else(String::new)
            ));

            // Create the temporary file
            let original_contents = fs::read_to_string(original_src_filepath).unwrap();
            let tmp_file = TempFile::new(tmp_lib_filepath.clone(), |lib_src_file| {
                let extra_code = r##"
mod cargo_apk_glue_code {
    use std::os::raw::c_void;

    // Exported function which is called be Android's NativeActivity
    #[no_mangle]
    pub unsafe extern "C" fn ANativeActivity_onCreate(
        activity: *mut c_void,
        saved_state: *mut c_void,
        saved_state_size: usize,
    ) {
        native_app_glue_onCreate(activity, saved_state, saved_state_size);
    }

    extern "C" {
        #[allow(non_snake_case)]
        fn native_app_glue_onCreate(
            activity: *mut c_void,
            saved_state: *mut c_void,
            saved_state_size: usize,
        );
    }

    #[no_mangle]
    extern "C" fn android_main(_app: *mut c_void) {
        let _ = super::main();
    }

    #[link(name = "android")]
    #[link(name = "log")]
    extern "C" {}
}"##;
                writeln!( lib_src_file, "{}\n{}", original_contents, extra_code)?;

                Ok(())
            }).map_err(|e| format_err!(
                "Unable to create temporary source file `{}`. Source directory must be writable. Cargo-apk creates temporary source files as part of the build process. {}.", tmp_lib_filepath.to_string_lossy(), e)
            )?;

            //
            // Replace source argument
            //
            let filename = path.file_name().unwrap().to_owned();
            let source_arg = new_args.iter_mut().find_map(|arg| {
                let path_arg = Path::new(&arg);
                let tmp = path_arg.file_name().unwrap();

                if filename == tmp {
                    Some(arg)
                } else {
                    None
                }
            });

            if let Some(source_arg) = source_arg {
                // Build a new relative path to the temporary source file and use it as the source argument
                // Using an absolute path causes compatibility issues in some cases under windows
                // If a UNC path is used then relative paths used in "include* macros" may not work if
                // the relative path includes "/" instead of "\"
                let path_arg = Path::new(&source_arg);
                let mut path_arg = path_arg.to_path_buf();
                path_arg.set_file_name(tmp_file.path.file_name().unwrap());
                *source_arg = path_arg.into_os_string();
            } else {
                return Err(format_err!(
                    "Unable to replace source argument when building target '{}'",
                    target.name()
                ));
            }

            //
            // Create output directory inside the build target directory
            //
            let build_path = self.build_target_dir.join("build");
            fs::create_dir_all(&build_path).unwrap();

            //
            // Change crate-type from bin to cdylib
            // Replace output directory with the directory we created
            //
            let mut iter = new_args.iter_mut().rev().peekable();
            while let Some(arg) = iter.next() {
                if let Some(prev_arg) = iter.peek() {
                    if *prev_arg == "--crate-type" && arg == "bin" {
                        *arg = "cdylib".into();
                    } else if *prev_arg == "--out-dir" {
                        *arg = build_path.clone().into();
                    }
                }
            }

            // Helper function to build arguments composed of concatenating two strings
            fn build_arg(start: &str, end: impl AsRef<OsStr>) -> OsString {
                let mut new_arg = OsString::new();
                new_arg.push(start);
                new_arg.push(end.as_ref());
                new_arg
            }

            // Determine paths
            let tool_root = util::llvm_toolchain_root(&self.config);
            let linker_path = tool_root
                .join("bin")
                .join(format!("{}-ld", &self.build_target.ndk_triple()));
            let sysroot = tool_root.join("sysroot");
            let version_independent_libraries_path = sysroot
                .join("usr")
                .join("lib")
                .join(&self.build_target.ndk_triple());
            let version_specific_libraries_path =
                util::find_ndk_path(self.config.min_sdk_version, |platform| {
                    version_independent_libraries_path.join(platform.to_string())
                })?;
            let gcc_lib_path = tool_root
                .join("lib/gcc")
                .join(&self.build_target.ndk_triple())
                .join("4.9.x");

            // Add linker arguments
            // Specify linker
            new_args.push(build_arg("-Clinker=", linker_path));

            // Set linker flavor
            new_args.push("-Clinker-flavor=ld".into());

            // Set system root
            new_args.push(build_arg("-Clink-arg=--sysroot=", sysroot));

            // Add version specific libraries directory to search path
            new_args.push(build_arg("-Clink-arg=-L", version_specific_libraries_path));

            // Add version independent libraries directory to search path
            new_args.push(build_arg(
                "-Clink-arg=-L",
                &version_independent_libraries_path,
            ));

            // Add path to folder containing libgcc.a to search path
            new_args.push(build_arg("-Clink-arg=-L", gcc_lib_path));

            // Add android native glue
            new_args.push(build_arg("-Clink-arg=", &self.android_native_glue_object));

            // Strip symbols for release builds
            if self.config.release {
                new_args.push("-Clink-arg=-strip-all".into());
            }

            // Require position independent code
            new_args.push("-Crelocation-model=pic".into());

            // Create new command
            let mut cmd = cmd.clone();
            cmd.args_replace(&new_args);

            //
            // Execute the command
            //
            cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
                .map(drop)?;

            // Execute the command again with the print flag to determine the name of the produced shared library and then add it to the list of shared librares to be added to the APK
            let stdout = cmd.arg("--print").arg("file-names").exec_with_output()?;
            let stdout = String::from_utf8(stdout.stdout).unwrap();
            let library_path = build_path.join(stdout.lines().next().unwrap());

            let mut shared_libraries = self.shared_libraries.lock().unwrap();
            shared_libraries.insert(
                target.into(),
                SharedLibrary {
                    abi: self.build_target,
                    path: library_path.clone(),
                    filename: format!("lib{}.so", target.name()),
                },
            );

            // If the target uses the C++ standard library, add the appropriate shared library
            // to the list of shared libraries to be added to the APK
            let readelf_path = util::find_readelf(&self.config, self.build_target)?;
            let readelf_output = process(readelf_path)
                .arg("-d")
                .arg(&library_path)
                .exec_with_output()?;
            use std::io::BufRead;
            let dynamically_links_to_cpp_standard_lib = readelf_output.stdout.lines().any(|l| {
                let l = l.as_ref().unwrap();
                l.contains("(NEEDED)") && l.contains("[libc++_shared.so]")
            });

            if dynamically_links_to_cpp_standard_lib {
                let cpp_library_path = version_independent_libraries_path.join("libc++_shared.so");
                shared_libraries.insert(
                    target.into(),
                    SharedLibrary {
                        abi: self.build_target,
                        path: cpp_library_path,
                        filename: "libc++_shared.so".into(),
                    },
                );
            }
        } else if mode == CompileMode::Test {
            // This occurs when --all-targets is specified
            eprintln!("Ignoring CompileMode::Test for target: {}", target.name());
        } else if mode == CompileMode::Build {
            let mut new_args = cmd.get_args().to_owned();

            //
            // Change crate-type from cdylib to rlib
            //
            let mut iter = new_args.iter_mut().rev().peekable();
            while let Some(arg) = iter.next() {
                if let Some(prev_arg) = iter.peek() {
                    if *prev_arg == "--crate-type" && arg == "cdylib" {
                        *arg = "rlib".into();
                    }
                }
            }

            let mut cmd = cmd.clone();
            cmd.args_replace(&new_args);
            cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
                .map(drop)?
        } else {
            cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
                .map(drop)?
        }

        Ok(())
    }
}

/// Returns the path to the ".c" file for the android native app glue
fn write_native_app_glue_src(android_artifacts_dir: &Path) -> CargoResult<PathBuf> {
    let output_dir = android_artifacts_dir.join("native_app_glue");
    fs::create_dir_all(&output_dir).unwrap();

    let mut h_file = File::create(output_dir.join("android_native_app_glue.h"))?;
    h_file.write_all(&include_bytes!("../../../native_app_glue/android_native_app_glue.h")[..])?;

    let c_path = output_dir.join("android_native_app_glue.c");
    let mut c_file = File::create(&c_path)?;
    c_file.write_all(&include_bytes!("../../../native_app_glue/android_native_app_glue.c")[..])?;

    Ok(c_path)
}

/// Returns the path to the built object file for the android native glue
fn build_android_native_glue(
    config: &AndroidConfig,
    android_native_glue_src_path: &PathBuf,
    build_target_dir: &PathBuf,
    build_target: AndroidBuildTarget,
) -> CargoResult<PathBuf> {
    let clang = util::find_clang(config, build_target)?;

    let android_native_glue_build_path = build_target_dir.join("android_native_glue");
    fs::create_dir_all(&android_native_glue_build_path)?;
    let android_native_glue_object_path =
        android_native_glue_build_path.join("android_native_glue.o");

    // Will produce warnings when bulding on linux? Create constants for extensions that can be used.. Or have separate functions?
    util::script_process(clang)
        .arg(android_native_glue_src_path)
        .arg("-c")
        .arg("-o")
        .arg(&android_native_glue_object_path)
        .exec()?;

    Ok(android_native_glue_object_path)
}

/// Write a CMake toolchain which will remove references to the rustc build target before including
/// the NDK provided toolchain. The NDK provided android toolchain will set the target appropriately
/// Returns the path to the generated toolchain file
fn write_cmake_toolchain(
    config: &AndroidConfig,
    build_target_dir: &PathBuf,
    build_target: AndroidBuildTarget,
) -> CargoResult<PathBuf> {
    let toolchain_path = build_target_dir.join("cargo-apk.toolchain.cmake");
    let mut toolchain_file = File::create(&toolchain_path).unwrap();
    writeln!(
        toolchain_file,
        r#"set(ANDROID_PLATFORM android-{min_sdk_version})
set(ANDROID_ABI {abi})
string(REPLACE "--target={build_target}" "" CMAKE_C_FLAGS "${{CMAKE_C_FLAGS}}")
string(REPLACE "--target={build_target}" "" CMAKE_CXX_FLAGS "${{CMAKE_CXX_FLAGS}}")
unset(CMAKE_C_COMPILER CACHE)
unset(CMAKE_CXX_COMPILER CACHE)
include("{ndk_path}/build/cmake/android.toolchain.cmake")"#,
        min_sdk_version = config.min_sdk_version,
        ndk_path = config.ndk_path.to_string_lossy().replace("\\", "/"), // Use forward slashes even on windows to avoid path escaping issues.
        build_target = build_target.rust_triple(),
        abi = build_target.android_abi(),
    )?;

    Ok(toolchain_path)
}