diff --git a/app/Commands/Extra/Compile.hs b/app/Commands/Extra/Compile.hs index c34a02d38..45f9b7ad4 100644 --- a/app/Commands/Extra/Compile.hs +++ b/app/Commands/Extra/Compile.hs @@ -73,32 +73,35 @@ prepareRuntime buildDir o = do ensureDir (includeDir parent filePath) BS.writeFile (toFilePath (includeDir filePath)) contents -outputFile :: Member App r => CompileOptions -> Path Abs File -> Sem r (Path Abs File) +outputFile :: forall r. Member App r => CompileOptions -> Path Abs File -> Sem r (Path Abs File) outputFile opts inputFile = - maybe (return defaultOutputFile) someBaseToAbs' (opts ^? compileOutputFile . _Just . pathPath) + maybe defaultOutputFile someBaseToAbs' (opts ^? compileOutputFile . _Just . pathPath) where - defaultOutputFile :: Path Abs File - defaultOutputFile = case opts ^. compileTarget of - TargetNative64 -> - if - | opts ^. compileCOutput -> replaceExtension' ".c" inputFile - | opts ^. compilePreprocess -> addExtension' ".c" (addExtension' ".out" (removeExtension' inputFile)) - | opts ^. compileAssembly -> replaceExtension' ".s" inputFile - | otherwise -> removeExtension' inputFile - TargetWasm32Wasi -> - if - | opts ^. compileCOutput -> replaceExtension' ".c" inputFile - | opts ^. compilePreprocess -> addExtension' ".c" (addExtension' ".out" (removeExtension' inputFile)) - | opts ^. compileAssembly -> replaceExtension' ".wat" inputFile - | otherwise -> replaceExtension' ".wasm" inputFile - TargetGeb -> - if - | opts ^. compileTerm -> replaceExtension' ".geb" inputFile - | otherwise -> replaceExtension' ".lisp" inputFile - TargetCore -> - replaceExtension' ".jvc" inputFile - TargetAsm -> - replaceExtension' ".jva" inputFile + defaultOutputFile :: Sem r (Path Abs File) + defaultOutputFile = do + invokeDir <- askInvokeDir + let baseOutputFile = invokeDir filename inputFile + return $ case opts ^. compileTarget of + TargetNative64 -> + if + | opts ^. compileCOutput -> replaceExtension' ".c" inputFile + | opts ^. compilePreprocess -> addExtension' ".c" (addExtension' ".out" (removeExtension' inputFile)) + | opts ^. compileAssembly -> replaceExtension' ".s" inputFile + | otherwise -> removeExtension' baseOutputFile + TargetWasm32Wasi -> + if + | opts ^. compileCOutput -> replaceExtension' ".c" inputFile + | opts ^. compilePreprocess -> addExtension' ".c" (addExtension' ".out" (removeExtension' inputFile)) + | opts ^. compileAssembly -> replaceExtension' ".wat" inputFile + | otherwise -> replaceExtension' ".wasm" baseOutputFile + TargetGeb -> + if + | opts ^. compileTerm -> replaceExtension' ".geb" inputFile + | otherwise -> replaceExtension' ".lisp" baseOutputFile + TargetCore -> + replaceExtension' ".jvc" baseOutputFile + TargetAsm -> + replaceExtension' ".jva" baseOutputFile clangNativeCompile :: forall r. diff --git a/tests/smoke/Commands/compile.smoke.yaml b/tests/smoke/Commands/compile.smoke.yaml index 008c42b31..fdb72561a 100644 --- a/tests/smoke/Commands/compile.smoke.yaml +++ b/tests/smoke/Commands/compile.smoke.yaml @@ -1,4 +1,4 @@ -working-directory: ./../../../tests +working-directory: ./../../../ tests: - name: shows-file-argument-for-autocompletion @@ -16,21 +16,34 @@ tests: shell: - bash script: | - cd ./../examples/milestone/HelloWorld + cd ./examples/milestone/HelloWorld juvix compile HelloWorld.juvix - [ -f HelloWorld ] ./HelloWorld exit-status: 0 stdout: | hello world! + - name: option-output-file + command: + shell: + - bash + script: | + temp=$(mktemp -d) + trap 'rm -rf -- "$temp"' EXIT + cd ./examples/milestone/HelloWorld + juvix compile -o $temp/Hello HelloWorld.juvix + $temp/Hello + exit-status: 0 + stdout: | + hello world! + - name: flag-internal-build-dir command: shell: - bash script: | temp=$(mktemp -d) - cd ./../examples/milestone/HelloWorld + cd ./examples/milestone/HelloWorld juvix compile HelloWorld.juvix --internal-build-dir "$temp" find "$temp" | wc -l rm -rf "$temp" @@ -38,3 +51,46 @@ tests: matches: | \s*([2-9]|[1-9][0-9]+) exit-status: 0 + + - name: default-output-file-in-invoke-dir + command: + shell: + - bash + script: | + temp=$(mktemp -d) + trap 'rm -rf -- "$temp"' EXIT + testdir=$PWD/examples/milestone/HelloWorld + cd $temp + juvix compile $testdir/HelloWorld.juvix + ./HelloWorld + stdout: | + hello world! + exit-status: 0 + + - name: target-wasm + command: + shell: + - bash + script: | + temp=$(mktemp -d) + trap 'rm -rf -- "$temp"' EXIT + testdir=$PWD/examples/milestone/HelloWorld + cd $temp + juvix compile -t wasm32-wasi $testdir/HelloWorld.juvix + [ -f HelloWorld.wasm ] + stdout: "" + exit-status: 0 + + - name: target-geb + command: + shell: + - bash + script: | + temp=$(mktemp -d) + trap 'rm -rf -- "$temp"' EXIT + testdir=$PWD/tests/Geb/positive/Compilation + cd $temp + juvix compile -t geb $testdir/test001.juvix + [ -f test001.lisp ] + stdout: "" + exit-status: 0