private StepExecutionResult executeMLBytecodeCompilation( ExecutionContext context, Path workingDirectory, ImmutableList<Path> sortedInput, ImmutableList.Builder<Path> linkerInputs) throws IOException, InterruptedException { MakeCleanDirectoryStep mkDir = new MakeCleanDirectoryStep(filesystem, ocamlContext.getCompileBytecodeOutputDir()); StepExecutionResult mkDirExecutionResult = mkDir.execute(context); if (!mkDirExecutionResult.isSuccess()) { return mkDirExecutionResult; } for (Path inputOutput : sortedInput) { String inputFileName = inputOutput.getFileName().toString(); String outputFileName = inputFileName .replaceFirst(OCamlCompilables.OCAML_ML_REGEX, OCamlCompilables.OCAML_CMO) .replaceFirst(OCamlCompilables.OCAML_RE_REGEX, OCamlCompilables.OCAML_CMO) .replaceFirst(OCamlCompilables.OCAML_MLI_REGEX, OCamlCompilables.OCAML_CMI) .replaceFirst(OCamlCompilables.OCAML_REI_REGEX, OCamlCompilables.OCAML_CMI); Path outputPath = ocamlContext.getCompileBytecodeOutputDir().resolve(outputFileName); if (!outputFileName.endsWith(OCamlCompilables.OCAML_CMI)) { linkerInputs.add(outputPath); } final ImmutableList<String> compileFlags = getCompileFlags(/* isBytecode */ true, /* excludeDeps */ false); Step compileBytecodeStep = new OCamlMLCompileStep( workingDirectory, resolver, new OCamlMLCompileStep.Args( filesystem.getAbsolutifier(), cCompilerEnvironment, cCompiler, ocamlContext.getOcamlBytecodeCompiler().get(), ocamlContext.getOCamlInteropIncludesDir(), outputPath, inputOutput, compileFlags)); StepExecutionResult compileExecutionResult = compileBytecodeStep.execute(context); if (!compileExecutionResult.isSuccess()) { return compileExecutionResult; } } return StepExecutionResult.SUCCESS; }
private int generateSources(ExecutionContext context, Path workingDirectory) throws IOException, InterruptedException { MakeCleanDirectoryStep mkDir = new MakeCleanDirectoryStep(filesystem, ocamlContext.getGeneratedSourceDir()); int mkDirExitCode = mkDir.execute(context); if (mkDirExitCode != 0) { return mkDirExitCode; } for (SourcePath yaccSource : ocamlContext.getYaccInput()) { SourcePath output = ocamlContext.getYaccOutput(ImmutableSet.of(yaccSource)).get(0); OCamlYaccStep yaccStep = new OCamlYaccStep( workingDirectory, resolver, new OCamlYaccStep.Args( ocamlContext.getYaccCompiler().get(), resolver.getAbsolutePath(output), resolver.getAbsolutePath(yaccSource))); int yaccExitCode = yaccStep.execute(context); if (yaccExitCode != 0) { return yaccExitCode; } } for (SourcePath lexSource : ocamlContext.getLexInput()) { SourcePath output = ocamlContext.getLexOutput(ImmutableSet.of(lexSource)).get(0); OCamlLexStep lexStep = new OCamlLexStep( workingDirectory, resolver, new OCamlLexStep.Args( ocamlContext.getLexCompiler().get(), resolver.getAbsolutePath(output), resolver.getAbsolutePath(lexSource))); int lexExitCode = lexStep.execute(context); if (lexExitCode != 0) { return lexExitCode; } } return 0; }
@Test public void testCreateAndRunGenrule() throws IOException, NoSuchBuildTargetException { /* * Programmatically build up a Genrule that corresponds to: * * genrule( * name = 'katana_manifest', * srcs = [ * 'convert_to_katana.py', * 'AndroidManifest.xml', * ], * cmd = 'python $SRCDIR/* > $OUT', * out = 'AndroidManifest.xml', * ) */ BuildRuleResolver ruleResolver = new BuildRuleResolver(); createSampleJavaBinaryRule(ruleResolver); Map<String, ?> instance = ImmutableMap.of( "name", "katana_manifest", "srcs", ImmutableList.<String>of("convert_to_katana.py", "AndroidManifest.xml"), "cmd", "python convert_to_katana.py AndroidManifest.xml > $OUT", "out", "AndroidManifest.xml", "deps", ImmutableList.<String>of("//java/com/facebook/util:util")); // From the Python object, create a GenruleBuildRuleFactory to create a Genrule.Builder // that builds a Genrule from the Python object. BuildTargetParser parser = EasyMock.createNiceMock(BuildTargetParser.class); EasyMock.expect( parser.parse( EasyMock.eq("//java/com/facebook/util:util"), EasyMock.anyObject(ParseContext.class))) .andStubReturn(BuildTargetFactory.newInstance("//java/com/facebook/util:util")); EasyMock.replay(parser); BuildTarget buildTarget = new BuildTarget("//src/com/facebook/katana", "katana_manifest"); BuildRuleFactoryParams params = NonCheckingBuildRuleFactoryParams.createNonCheckingBuildRuleFactoryParams( instance, parser, buildTarget); GenruleBuildRuleFactory factory = new GenruleBuildRuleFactory(); Builder builder = factory.newInstance(params); builder.setRelativeToAbsolutePathFunctionForTesting(relativeToAbsolutePathFunction); Genrule genrule = ruleResolver.buildAndAddToIndex(builder); // Verify all of the observers of the Genrule. assertEquals(BuildRuleType.GENRULE, genrule.getType()); assertEquals( GEN_DIR + "/src/com/facebook/katana/AndroidManifest.xml", genrule.getPathToOutputFile()); assertEquals( getAbsolutePathInBase(GEN_DIR + "/src/com/facebook/katana/AndroidManifest.xml").toString(), genrule.getAbsoluteOutputFilePath()); BuildContext buildContext = null; // unused since there are no deps ImmutableSortedSet<String> inputsToCompareToOutputs = ImmutableSortedSet.of( "src/com/facebook/katana/convert_to_katana.py", "src/com/facebook/katana/AndroidManifest.xml"); assertEquals(inputsToCompareToOutputs, genrule.getInputsToCompareToOutput()); // Verify that the shell commands that the genrule produces are correct. List<Step> steps = genrule.getBuildSteps(buildContext, new FakeBuildableContext()); assertEquals(7, steps.size()); Step firstStep = steps.get(0); assertTrue(firstStep instanceof RmStep); RmStep rmCommand = (RmStep) firstStep; ExecutionContext executionContext = newEmptyExecutionContext(); assertEquals( "First command should delete the output file to be written by the genrule.", ImmutableList.of("rm", "-f", GEN_DIR + "/src/com/facebook/katana/AndroidManifest.xml"), rmCommand.getShellCommand(executionContext)); Step secondStep = steps.get(1); assertTrue(secondStep instanceof MkdirStep); MkdirStep mkdirCommand = (MkdirStep) secondStep; assertEquals( "Second command should make sure the output directory exists.", Paths.get(GEN_DIR + "/src/com/facebook/katana"), mkdirCommand.getPath(executionContext)); Step mkTmpDir = steps.get(2); assertTrue(mkTmpDir instanceof MakeCleanDirectoryStep); MakeCleanDirectoryStep secondMkdirCommand = (MakeCleanDirectoryStep) mkTmpDir; String pathToTmpDir = GEN_DIR + "/src/com/facebook/katana/katana_manifest__tmp"; assertEquals( "Third command should create the temp directory to be written by the genrule.", pathToTmpDir, secondMkdirCommand.getPath()); Step mkSrcDir = steps.get(3); assertTrue(mkSrcDir instanceof MakeCleanDirectoryStep); MakeCleanDirectoryStep thirdMkdirCommand = (MakeCleanDirectoryStep) mkTmpDir; String pathToSrcDir = GEN_DIR + "/src/com/facebook/katana/katana_manifest__srcs"; assertEquals( "Fourth command should create the temp source directory to be written by the genrule.", pathToTmpDir, thirdMkdirCommand.getPath()); MkdirAndSymlinkFileStep linkSource1 = (MkdirAndSymlinkFileStep) steps.get(4); assertEquals("src/com/facebook/katana/convert_to_katana.py", linkSource1.getSource()); assertEquals(pathToSrcDir + "/convert_to_katana.py", linkSource1.getTarget()); MkdirAndSymlinkFileStep linkSource2 = (MkdirAndSymlinkFileStep) steps.get(5); assertEquals("src/com/facebook/katana/AndroidManifest.xml", linkSource2.getSource()); assertEquals(pathToSrcDir + "/AndroidManifest.xml", linkSource2.getTarget()); Step sixthStep = steps.get(6); assertTrue(sixthStep instanceof ShellStep); ShellStep genruleCommand = (ShellStep) sixthStep; assertEquals("genrule", genruleCommand.getShortName()); assertEquals( ImmutableMap.<String, String>builder() .put( "OUT", getAbsolutePathInBase(GEN_DIR + "/src/com/facebook/katana/AndroidManifest.xml") .toString()) .build(), genruleCommand.getEnvironmentVariables(executionContext)); assertEquals( ImmutableList.of( "/bin/bash", "-e", "-c", "python convert_to_katana.py AndroidManifest.xml > $OUT"), genruleCommand.getShellCommand(executionContext)); }