Skip to content

Instantly share code, notes, and snippets.

@objectx
Last active August 28, 2019 14:06
Show Gist options
  • Save objectx/dbaecc66865f083c19a617138937d567 to your computer and use it in GitHub Desktop.
Save objectx/dbaecc66865f083c19a617138937d567 to your computer and use it in GitHub Desktop.
Fake script for C++ (with conan)
#r "paket: groupref Fake //"
#load ".fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.Core.TargetOperators
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open BlackFox.CommandLine
Target.initEnvironment()
let buildRoot = __SOURCE_DIRECTORY__ </> "B"
let profile = "clang-8"
let buildType = "Release"
let buildDirectory prof typ =
buildRoot </> (sprintf "cmake-%s-%s" (typ |> String.toLower) prof)
let getCompiler prof key =
let parser (r: ProcessOutput) =
r.Output |> String.trim
CmdLine.empty
|> CmdLine.append "profile"
|> CmdLine.append "get"
|> CmdLine.appendf "env.%s" key
|> CmdLine.append prof
|> CmdLine.toArray
|> CreateProcess.fromRawCommand "conan"
|> CreateProcess.ensureExitCode
|> CreateProcess.redirectOutput
|> CreateProcess.mapResult parser
|> Proc.run
Target.create "EnsureBuildRoot" <| fun _ ->
Directory.ensure buildRoot
let tagFile = buildRoot </> ".BUILDDIR.TAG"
use f = System.IO.File.CreateText(tagFile)
f.WriteLine("build-dir: 19BBC6F7-3FBF-49BD-BAA4-EA60CE5A0735")
f.WriteLine("# Tag for `restic`")
Target.create "Clean" <| fun _ ->
buildDirectory profile buildType
|> Directory.delete
Target.create "ConanSetup" <| fun _ ->
let buildDir = buildDirectory profile buildType
buildDir |> Directory.ensure
CmdLine.empty
|> CmdLine.append "install"
|> CmdLine.appendPrefix "--profile" profile
|> CmdLine.appendPrefix "--build" "missing"
|> CmdLine.append __SOURCE_DIRECTORY__
|> CmdLine.toArray
|> CreateProcess.fromRawCommand "conan"
|> CreateProcess.ensureExitCode
|> CreateProcess.withWorkingDirectory buildDir
|> Proc.run
|> ignore
"EnsureBuildRoot" ==> "ConanSetup"
Target.create "Configure" <| fun _ ->
let buildDir = buildDirectory profile buildType
let cc = getCompiler profile "CC"
let cxx = getCompiler profile "CXX"
Trace.logfn "CC = %s, CXX = %s" cc.Result cxx.Result
CmdLine.empty
|> CmdLine.appendPrefix "-S" __SOURCE_DIRECTORY__
|> CmdLine.appendPrefix "-B" buildDir
|> CmdLine.appendPrefix "-G" "CodeBlocks - Unix Makefiles"
|> CmdLine.appendPrefixf "-D" "CMAKE_C_COMPILER=%s" cc.Result
|> CmdLine.appendPrefixf "-D" "CMAKE_CXX_COMPILER=%s" cxx.Result
|> CmdLine.appendPrefixf "-D" "CMAKE_BUILD_TYPE=%s" buildType
|> CmdLine.appendPrefix "-D" "CMAKE_EXPORT_COMPILE_COMMANDS=YES"
|> CmdLine.toArray
|> CreateProcess.fromRawCommand "cmake"
|> CreateProcess.ensureExitCode
|> CreateProcess.withWorkingDirectory __SOURCE_DIRECTORY__
|> Proc.run
|> ignore
"EnsureBuildRoot" ==> "Configure"
Target.create "Build" <| fun _ ->
let buildDir = buildDirectory profile buildType
CmdLine.empty
|> CmdLine.appendPrefix "--build" buildDir
|> CmdLine.appendPrefix "-j" "8"
|> CmdLine.toArray
|> CreateProcess.fromRawCommand "cmake"
|> CreateProcess.ensureExitCode
|> CreateProcess.withWorkingDirectory __SOURCE_DIRECTORY__
|> Proc.run
|> ignore
"ConanSetup"
==> "Configure"
==> "Build"
Target.runOrList()
group Fake
version 5.216.0
source https://api.nuget.org/v3/index.json
storage: none
nuget FSharp.Core ~> 4 prerelease
nuget Fake.Core.Target ~> 5 prerelease
nuget Blackfox.CommandLine ~> 1 prerelease
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment