Search in sources :

Example 1 with Preprocessor

use of com.facebook.buck.cxx.Preprocessor in project buck by facebook.

the class NdkLibraryDescription method generateMakefile.

/**
   * @return a {@link BuildRule} which generates a Android.mk which pulls in the local Android.mk
   *     file and also appends relevant preprocessor and linker flags to use C/C++ library deps.
   */
private Pair<String, Iterable<BuildRule>> generateMakefile(BuildRuleParams params, BuildRuleResolver resolver) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    ImmutableList.Builder<String> outputLinesBuilder = ImmutableList.builder();
    ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
    for (Map.Entry<NdkCxxPlatforms.TargetCpuType, NdkCxxPlatform> entry : cxxPlatforms.entrySet()) {
        CxxPlatform cxxPlatform = entry.getValue().getCxxPlatform();
        // Collect the preprocessor input for all C/C++ library deps.  We search *through* other
        // NDK library rules.
        CxxPreprocessorInput cxxPreprocessorInput = CxxPreprocessorInput.concat(CxxPreprocessables.getTransitiveCxxPreprocessorInput(cxxPlatform, params.getDeps(), NdkLibrary.class::isInstance));
        // We add any dependencies from the C/C++ preprocessor input to this rule, even though
        // it technically should be added to the top-level rule.
        deps.addAll(cxxPreprocessorInput.getDeps(resolver, ruleFinder));
        // Add in the transitive preprocessor flags contributed by C/C++ library rules into the
        // NDK build.
        ImmutableList.Builder<String> ppFlags = ImmutableList.builder();
        ppFlags.addAll(cxxPreprocessorInput.getPreprocessorFlags().get(CxxSource.Type.C));
        Preprocessor preprocessor = CxxSourceTypes.getPreprocessor(cxxPlatform, CxxSource.Type.C).resolve(resolver);
        ppFlags.addAll(CxxHeaders.getArgs(cxxPreprocessorInput.getIncludes(), pathResolver, Optional.empty(), preprocessor));
        String localCflags = Joiner.on(' ').join(escapeForMakefile(params.getProjectFilesystem(), ppFlags.build()));
        // Collect the native linkable input for all C/C++ library deps.  We search *through* other
        // NDK library rules.
        NativeLinkableInput nativeLinkableInput = NativeLinkables.getTransitiveNativeLinkableInput(cxxPlatform, params.getDeps(), Linker.LinkableDepType.SHARED, NdkLibrary.class::isInstance);
        // We add any dependencies from the native linkable input to this rule, even though
        // it technically should be added to the top-level rule.
        deps.addAll(nativeLinkableInput.getArgs().stream().flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator());
        // Add in the transitive native linkable flags contributed by C/C++ library rules into the
        // NDK build.
        String localLdflags = Joiner.on(' ').join(escapeForMakefile(params.getProjectFilesystem(), com.facebook.buck.rules.args.Arg.stringify(nativeLinkableInput.getArgs(), pathResolver)));
        // Write the relevant lines to the generated makefile.
        if (!localCflags.isEmpty() || !localLdflags.isEmpty()) {
            NdkCxxPlatforms.TargetCpuType targetCpuType = entry.getKey();
            String targetArchAbi = getTargetArchAbi(targetCpuType);
            outputLinesBuilder.add(String.format("ifeq ($(TARGET_ARCH_ABI),%s)", targetArchAbi));
            if (!localCflags.isEmpty()) {
                outputLinesBuilder.add("BUCK_DEP_CFLAGS=" + localCflags);
            }
            if (!localLdflags.isEmpty()) {
                outputLinesBuilder.add("BUCK_DEP_LDFLAGS=" + localLdflags);
            }
            outputLinesBuilder.add("endif");
            outputLinesBuilder.add("");
        }
    }
    // GCC-only magic that rewrites non-deterministic parts of builds
    String ndksubst = NdkCxxPlatforms.ANDROID_NDK_ROOT;
    outputLinesBuilder.addAll(ImmutableList.copyOf(new String[] { // We're evaluated once per architecture, but want to add the cflags only once.
    "ifeq ($(BUCK_ALREADY_HOOKED_CFLAGS),)", "BUCK_ALREADY_HOOKED_CFLAGS := 1", // Only GCC supports -fdebug-prefix-map
    "ifeq ($(filter clang%,$(NDK_TOOLCHAIN_VERSION)),)", // Replace absolute paths with machine-relative ones.
    "NDK_APP_CFLAGS += -fdebug-prefix-map=$(NDK_ROOT)/=" + ndksubst + "/", "NDK_APP_CFLAGS += -fdebug-prefix-map=$(abspath $(BUCK_PROJECT_DIR))/=./", // repository root.
    "NDK_APP_CFLAGS += -fdebug-prefix-map=$(BUCK_PROJECT_DIR)/=./", "NDK_APP_CFLAGS += -fdebug-prefix-map=./=" + ".$(subst $(abspath $(BUCK_PROJECT_DIR)),,$(abspath $(CURDIR)))/", "NDK_APP_CFLAGS += -fno-record-gcc-switches", "ifeq ($(filter 4.6,$(TOOLCHAIN_VERSION)),)", // headers either.
    "NDK_APP_CPPFLAGS += -fno-canonical-system-headers", // detailed command line argument information anyway.
    "NDK_APP_CFLAGS += -gno-record-gcc-switches", // !GCC 4.6
    "endif", // !clang
    "endif", // absolute path, but only for modules under the project root.
    "BUCK_SAVED_IMPORTS := $(__ndk_import_dirs)", "__ndk_import_dirs :=", "$(foreach __dir,$(BUCK_SAVED_IMPORTS),\\", "$(call import-add-path-optional,\\", "$(if $(filter $(abspath $(BUCK_PROJECT_DIR))%,$(__dir)),\\", "$(BUCK_PROJECT_DIR)$(patsubst $(abspath $(BUCK_PROJECT_DIR))%,%,$(__dir)),\\", "$(__dir))))", // !already hooked
    "endif", // generic paths.
    "NDK_APP_CFLAGS += -fdebug-prefix-map=$(TOOLCHAIN_PREBUILT_ROOT)/=" + "@ANDROID_NDK_ROOT@/toolchains/$(TOOLCHAIN_NAME)/prebuilt/@BUILD_HOST@/" }));
    outputLinesBuilder.add("include Android.mk");
    String contents = Joiner.on(System.lineSeparator()).join(outputLinesBuilder.build());
    return new Pair<String, Iterable<BuildRule>>(contents, deps.build());
}
Also used : CxxPlatform(com.facebook.buck.cxx.CxxPlatform) ImmutableList(com.google.common.collect.ImmutableList) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) NativeLinkableInput(com.facebook.buck.cxx.NativeLinkableInput) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule) CxxPreprocessorInput(com.facebook.buck.cxx.CxxPreprocessorInput) Preprocessor(com.facebook.buck.cxx.Preprocessor) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(com.facebook.buck.model.Pair)

Aggregations

CxxPlatform (com.facebook.buck.cxx.CxxPlatform)1 CxxPreprocessorInput (com.facebook.buck.cxx.CxxPreprocessorInput)1 NativeLinkableInput (com.facebook.buck.cxx.NativeLinkableInput)1 Preprocessor (com.facebook.buck.cxx.Preprocessor)1 Pair (com.facebook.buck.model.Pair)1 BuildRule (com.facebook.buck.rules.BuildRule)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 Map (java.util.Map)1