Maciek
Maciek

Reputation: 1716

How to build F# application with command line

My F# project consists of three files, and has dependencies to two 3rd party DLLs. Currently, I use a VS2019 project with NuGet package dependencies, and I am able to build and run the project using VS2019 commands. I am also able to build and run my app using command line and dotnet run MyProject.fsproj command. Currently it's a dotnet core 3.1 project, but it does not necessarily have to be such: the most important part for me is to be able to have three source files: two with my implementation, and one with main entry point.

My problem is, I need to build and run the project in a setup I cannot control, with a severely constrained quota of execution time of a couple of seconds (approximately 10s). I am trying to squeeze out every easily accessible second and I wonder if it would be possible to compile and build the project without an overhead of complex build process of dotnet build and MSBuild, invoking fsc.exe directly. However, I cannot even tests the idea because I am not able to get a runnable executable right.

Looking at the logs of VS, I found it uses following command to run fsc.exe:

"c:\program files (x86)\microsoft visual studio\2019\enterprise\common7\ide\commonextensions\microsoft\fsharp\Tools\fsc.exe" -o:obj\Debug\netcoreapp3.1\MyProject.dll ^
-g ^
--debug:portable ^
--noframework ^
--define:TRACE ^
--define:DEBUG ^
--define:NETCOREAPP ^
--define:NETCOREAPP3_1 ^
--define:NETCOREAPP1_0_OR_GREATER ^
--define:NETCOREAPP1_1_OR_GREATER ^
--define:NETCOREAPP2_0_OR_GREATER ^
--define:NETCOREAPP2_1_OR_GREATER ^
--define:NETCOREAPP2_2_OR_GREATER ^
--define:NETCOREAPP3_0_OR_GREATER ^
--define:NETCOREAPP3_1_OR_GREATER ^
--optimize- ^
--tailcalls- ^
-r: ...many references to framework assemblies and 3rd party assemblies... ^
--target:exe ^
--warn:3 ^
--warnaserror:3239 ^
--fullpaths ^
--flaterrors ^
--highentropyva+ ^
--targetprofile:netcore ^
--nocopyfsharpcore ^
--deterministic+ ^
--simpleresolution ^
file1.fs ^
file2.fs ^
Program.fs

I am able to run the above command line, but I do not understand its output and I do not know how to proceed from there: the command creates files named MyProject.dll and MyProject.pdb. Resulting file is not an exe, and is missing its dependencies. I see that VS performs some additional build steps of copying/renaming a file called apphost.exe but I do not know why it does so, and how to do it from command line.
My dependencies do not have to be NuGet packages, I can just download the dependencies and place them somewhere in my environment.
I saw the --standalone compiler option but as far as I see, it links all dependencies statically, but still outputs MyProject.dll and not an executable.

My questions would be then: how do I build an F# project with command line, and get a runnable version of it, with all dependencies resolved and executables linked, everything collected so I can just run it with MyProject.exe or dotnet MyProject.dll, or some other command? Is there a chance that building "manually" will save me some time compared to just calling dotnet run MyProject.fsproj?

Upvotes: 2

Views: 345

Answers (1)

Phillip Carter
Phillip Carter

Reputation: 5005

My questions would be then: how do I build an F# project with command line, and get a runnable version of it, with all dependencies resolved and executables linked, everything collected so I can just run it with MyProject.exe or dotnet MyProject.dll, or some other command?

This is what the build system, invoked with dotnet build, does. If you try to piece this together manually all you'll end up doing is recreate what it already does for you.

Upvotes: 1

Related Questions