Search in sources :

Example 1 with BuildConfigFields

use of com.facebook.buck.rules.coercer.BuildConfigFields in project buck by facebook.

the class AndroidBinaryGraphEnhancer method addBuildConfigDeps.

/**
   * If the user specified any android_build_config() rules, then we must add some build rules to
   * generate the production {@code BuildConfig.class} files and ensure that they are included in
   * the list of {@link AndroidPackageableCollection#getClasspathEntriesToDex}.
   */
public static ImmutableSortedSet<JavaLibrary> addBuildConfigDeps(BuildRuleParams originalParams, PackageType packageType, EnumSet<ExopackageMode> exopackageModes, BuildConfigFields buildConfigValues, Optional<SourcePath> buildConfigValuesFile, BuildRuleResolver ruleResolver, JavacOptions javacOptions, AndroidPackageableCollection packageableCollection) throws NoSuchBuildTargetException {
    ImmutableSortedSet.Builder<JavaLibrary> result = ImmutableSortedSet.naturalOrder();
    BuildConfigFields buildConfigConstants = BuildConfigFields.fromFields(ImmutableList.of(BuildConfigFields.Field.of("boolean", BuildConfigs.DEBUG_CONSTANT, String.valueOf(packageType != AndroidBinary.PackageType.RELEASE)), BuildConfigFields.Field.of("boolean", BuildConfigs.IS_EXO_CONSTANT, String.valueOf(!exopackageModes.isEmpty())), BuildConfigFields.Field.of("int", BuildConfigs.EXOPACKAGE_FLAGS, String.valueOf(ExopackageMode.toBitmask(exopackageModes)))));
    for (Map.Entry<String, BuildConfigFields> entry : packageableCollection.getBuildConfigs().entrySet()) {
        // Merge the user-defined constants with the APK-specific overrides.
        BuildConfigFields totalBuildConfigValues = BuildConfigFields.empty().putAll(entry.getValue()).putAll(buildConfigValues).putAll(buildConfigConstants);
        // Each enhanced dep needs a unique build target, so we parameterize the build target by the
        // Java package.
        String javaPackage = entry.getKey();
        Flavor flavor = InternalFlavor.of("buildconfig_" + javaPackage.replace('.', '_'));
        BuildTarget buildTargetWithFlavors = BuildTarget.builder(originalParams.getBuildTarget()).addFlavors(flavor).build();
        BuildRuleParams buildConfigParams = new BuildRuleParams(buildTargetWithFlavors, /* declaredDeps */
        Suppliers.ofInstance(ImmutableSortedSet.of()), /* extraDeps */
        Suppliers.ofInstance(ImmutableSortedSet.of()), originalParams.getProjectFilesystem(), originalParams.getCellRoots());
        JavaLibrary buildConfigJavaLibrary = AndroidBuildConfigDescription.createBuildRule(buildConfigParams, javaPackage, totalBuildConfigValues, buildConfigValuesFile, /* useConstantExpressions */
        true, javacOptions, ruleResolver);
        ruleResolver.addToIndex(buildConfigJavaLibrary);
        Preconditions.checkNotNull(buildConfigJavaLibrary.getSourcePathToOutput(), "%s must have an output file.", buildConfigJavaLibrary);
        result.add(buildConfigJavaLibrary);
    }
    return result.build();
}
Also used : JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields)

Example 2 with BuildConfigFields

use of com.facebook.buck.rules.coercer.BuildConfigFields in project buck by facebook.

the class AndroidBuildConfigJavaLibraryTest method testBuildConfigHasCorrectProperties.

@Test
public void testBuildConfigHasCorrectProperties() throws NoSuchBuildTargetException {
    BuildRuleParams params = new FakeBuildRuleParamsBuilder("//foo:bar").build();
    BuildConfigFields fields = BuildConfigFields.fromFieldDeclarations(Collections.singleton("String KEY = \"value\""));
    BuildRuleResolver buildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    AndroidBuildConfigJavaLibrary buildConfigJavaLibrary = AndroidBuildConfigDescription.createBuildRule(params, "com.example.buck", /* values */
    fields, /* valuesFile */
    Optional.empty(), /* useConstantExpressions */
    false, DEFAULT_JAVAC_OPTIONS, buildRuleResolver);
    AndroidBuildConfig buildConfig = buildConfigJavaLibrary.getAndroidBuildConfig();
    assertEquals("com.example.buck", buildConfig.getJavaPackage());
    assertEquals(fields, buildConfig.getBuildConfigFields());
}
Also used : BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields) Test(org.junit.Test)

Example 3 with BuildConfigFields

use of com.facebook.buck.rules.coercer.BuildConfigFields in project buck by facebook.

the class BuildConfigsTest method testCustomGenerateBuildConfigDotJavaWithoutConstantExpressions.

@Test
public void testCustomGenerateBuildConfigDotJavaWithoutConstantExpressions() {
    BuildConfigFields customValues = BuildConfigFields.fromFieldDeclarations(ImmutableList.of("String KEYSTORE_TYPE = \"release\"", "int BUILD_NUMBER = 42", "long BUILD_DATE = 1404321113076000L", "float THREE = 3.0F", "boolean DEBUG = false", "boolean IS_EXOPACKAGE = true"));
    String expectedJavaCode = "// Generated by //java/com/example:build_config. DO NOT MODIFY.\n" + "package com.example;\n" + "public class BuildConfig {\n" + "  private BuildConfig() {}\n" + "  public static final String KEYSTORE_TYPE = " + "!Boolean.parseBoolean(null) ? \"release\" : null;\n" + "  public static final int BUILD_NUMBER = " + "!Boolean.parseBoolean(null) ? 42 : 0;\n" + "  public static final long BUILD_DATE = " + "!Boolean.parseBoolean(null) ? 1404321113076000L : 0;\n" + "  public static final float THREE = " + "!Boolean.parseBoolean(null) ? 3.0F : 0;\n" + "  public static final boolean DEBUG = Boolean.parseBoolean(null);\n" + "  public static final boolean IS_EXOPACKAGE = !Boolean.parseBoolean(null);\n" + "  public static final int EXOPACKAGE_FLAGS = !Boolean.parseBoolean(null) ? 0 : 0;\n" + "}\n";
    UnflavoredBuildTarget source = BuildTargetFactory.newInstance("//java/com/example:build_config").getUnflavoredBuildTarget();
    String observedJavaCode = BuildConfigs.generateBuildConfigDotJava(source, "com.example", /* useConstantExpressions */
    false, customValues);
    assertEquals(expectedJavaCode, observedJavaCode);
}
Also used : UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields) Test(org.junit.Test)

Example 4 with BuildConfigFields

use of com.facebook.buck.rules.coercer.BuildConfigFields in project buck by facebook.

the class AndroidBuildConfig method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    Supplier<BuildConfigFields> totalFields;
    if (valuesFile.isPresent()) {
        final ReadValuesStep readValuesStep = new ReadValuesStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(valuesFile.get()));
        steps.add(readValuesStep);
        totalFields = Suppliers.memoize(() -> defaultValues.putAll(readValuesStep.get()));
    } else {
        totalFields = Suppliers.ofInstance(defaultValues);
    }
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToOutputFile.getParent()));
    steps.add(new GenerateBuildConfigStep(getProjectFilesystem(), getBuildTarget().getUnflavoredBuildTarget(), javaPackage, useConstantExpressions, totalFields, pathToOutputFile));
    buildableContext.recordArtifact(pathToOutputFile);
    return steps.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Step(com.facebook.buck.step.Step) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields)

Example 5 with BuildConfigFields

use of com.facebook.buck.rules.coercer.BuildConfigFields in project buck by facebook.

the class BuildConfigsTest method testCustomGenerateBuildConfigDotJavaWithConstantExpressions.

@Test
public void testCustomGenerateBuildConfigDotJavaWithConstantExpressions() {
    BuildConfigFields customValues = BuildConfigFields.fromFieldDeclarations(ImmutableList.of("String KEYSTORE_TYPE = \"release\"", "int BUILD_NUMBER = 42", "long BUILD_DATE = 1404321113076000L", "float THREE = 3.0F", "boolean DEBUG = false", "int EXOPACKAGE_FLAGS = 0"));
    String expectedJavaCode = "// Generated by //java/com/example:build_config. DO NOT MODIFY.\n" + "package com.example;\n" + "public class BuildConfig {\n" + "  private BuildConfig() {}\n" + "  public static final String KEYSTORE_TYPE = \"release\";\n" + "  public static final int BUILD_NUMBER = 42;\n" + "  public static final long BUILD_DATE = 1404321113076000L;\n" + "  public static final float THREE = 3.0F;\n" + "  public static final boolean DEBUG = false;\n" + "  public static final int EXOPACKAGE_FLAGS = 0;\n" + "  public static final boolean IS_EXOPACKAGE = false;\n" + "}\n";
    UnflavoredBuildTarget source = BuildTargetFactory.newInstance("//java/com/example:build_config").getUnflavoredBuildTarget();
    String observedJavaCode = BuildConfigs.generateBuildConfigDotJava(source, "com.example", /* useConstantExpressions */
    true, customValues);
    assertEquals(expectedJavaCode, observedJavaCode);
}
Also used : UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildConfigFields(com.facebook.buck.rules.coercer.BuildConfigFields) Test(org.junit.Test)

Aggregations

BuildConfigFields (com.facebook.buck.rules.coercer.BuildConfigFields)5 Test (org.junit.Test)3 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)2 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)2 DefaultJavaLibrary (com.facebook.buck.jvm.java.DefaultJavaLibrary)1 JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 Flavor (com.facebook.buck.model.Flavor)1 InternalFlavor (com.facebook.buck.model.InternalFlavor)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)1 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)1 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)1 Step (com.facebook.buck.step.Step)1 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 Map (java.util.Map)1