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();
}
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());
}
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);
}
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();
}
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);
}
Aggregations