Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for CMake configuration and build #785

Merged
merged 1 commit into from
May 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Samples/CMakeSupport/Advanced/Communicator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required (VERSION 2.6)

project (Communicator CXX)

include_directories ("${PROJECT_SOURCE_DIR}")
add_library (Communicator Communicator.cxx)

install (TARGETS Communicator
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
COMPONENT library
)
install (FILES Communicator.h DESTINATION include)
13 changes: 13 additions & 0 deletions Samples/CMakeSupport/Advanced/Communicator/Communicator.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Communicator.h"

using namespace std;

Communicator::Communicator(const string & interlocutorName) :
_interlocutorName(interlocutorName)
{
}

string Communicator::hello() const
{
return "Hello " + _interlocutorName + "!";
}
17 changes: 17 additions & 0 deletions Samples/CMakeSupport/Advanced/Communicator/Communicator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef COMMUNICATE_H
#define COMMUNICATE_H

#include <string>

// Provides helpers for communicating with an end-user.
class Communicator
{
private:
std::string _interlocutorName;

public:
explicit Communicator(const std::string & interlocutorName);
std::string hello() const;
};

#endif // COMMUNICATE_H
14 changes: 14 additions & 0 deletions Samples/CMakeSupport/Advanced/Hello/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required (VERSION 2.6)

project (Hello CXX)

# Not using FindLibrary to keep the example simple.
if (NOT DEFINED COMMUNICATOR_INCLUDE_DIR OR NOT DEFINED COMMUNICATOR_LIBRARY)
message (FATAL_ERROR "Please define both COMMUNICATOR_INCLUDE_DIR and COMMUNICATOR_LIBRARY.")
endif ()

include_directories ("${COMMUNICATOR_INCLUDE_DIR}")

add_executable (hello main.cxx)

target_link_libraries (hello "${COMMUNICATOR_LIBRARY}")
20 changes: 20 additions & 0 deletions Samples/CMakeSupport/Advanced/Hello/main.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <cstdlib>
#include <iostream>
#include <Communicator.h>

using namespace std;

int main(int argc, char ** argv)
{
if (argc < 2)
{
cerr << "ERROR: Please provide at least one name as argument." << endl;
return EXIT_FAILURE;
}
for (int i = 1; i < argc; i++)
{
Communicator communicator(argv[i]);
cout << communicator.hello() << endl;
}
return EXIT_SUCCESS;
}
95 changes: 95 additions & 0 deletions Samples/CMakeSupport/Advanced/build.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#r "../../../build/FakeLib.dll"
open Fake

// Out of source build: CMake separates the source code from what's being generated.

let communicatorSourceDir = currentDirectory @@ "Communicator"
let communicatorBinaryDir = communicatorSourceDir + "-build"
let communicatorInstallDir = communicatorSourceDir + "-install"
let helloSourceDir = currentDirectory @@ "Hello"
let helloBinaryDir = helloSourceDir + "-build"

// As both CMake projects will use the same compilers, we define the common parameters only once.

let commonGenerateParams (parameters:CMakeGenerateParams) = {
parameters with
Generator = if isUnix then "Unix Makefiles" else "Visual Studio 12 2013"
Variables =
if not isUnix then parameters.Variables else
[{Name = "CMAKE_BUILD_TYPE"; Value = CMakeString("Release")}]
}

let commonBuildParams (parameters:CMakeBuildParams) = {
parameters with
Config = if isUnix then parameters.Config else "Release"
}

// Clean both build directories to ensure a reproducible output between each run.
Target "Clean" (fun _ ->
CleanDirs [ communicatorBinaryDir; communicatorInstallDir; helloBinaryDir ]
)

Target "Configure-Communicator" (fun _ ->
ensureDirectory communicatorBinaryDir
CMake.Generate (fun p ->
{ commonGenerateParams p with
SourceDirectory = communicatorSourceDir
BinaryDirectory = communicatorBinaryDir
InstallDirectory = communicatorInstallDir
}
)
)

Target "Build-Communicator" (fun _ ->
CMake.Build (fun p ->
{ commonBuildParams p with
BinaryDirectory = communicatorBinaryDir
}
)
)

Target "Install-Communicator" (fun _ ->
CMake.Build (fun p ->
{ commonBuildParams p with
BinaryDirectory = communicatorBinaryDir
Target = if isUnix then "install" else "INSTALL"
}
)
)

Target "Configure-Hello" (fun _ ->
ensureDirectory helloBinaryDir
CMake.Generate (fun p ->
{ commonGenerateParams p with
SourceDirectory = helloSourceDir
BinaryDirectory = helloBinaryDir
Variables =
[
{Name = "COMMUNICATOR_INCLUDE_DIR";
Value = CMakeDirPath(communicatorInstallDir @@ "include")}
{Name = "COMMUNICATOR_LIBRARY";
Value = CMakeFilePath(communicatorInstallDir @@ "lib" @@ "Communicator.lib")}
]
}
)
)

Target "Build-Hello" (fun _ ->
CMake.Build (fun p ->
{ commonBuildParams p with
BinaryDirectory = helloBinaryDir
}
)
)

Target "All" (fun _ -> ())

"Clean"
==> "Configure-Communicator"
==> "Build-Communicator"
==> "Install-Communicator"
==> "Configure-Hello"
==> "Build-Hello"
"Build-Hello" ==> "All"

RunTargetOrDefault "All"
5 changes: 5 additions & 0 deletions Samples/CMakeSupport/Simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required (VERSION 2.6)

project (Hello CXX)

add_executable (hello main.cxx)
34 changes: 34 additions & 0 deletions Samples/CMakeSupport/Simple/build.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#r "../../../build/FakeLib.dll"
open Fake

// Out of source build: CMake separates the source code from what's being generated.
let binaryDir = "./build"

// Clean both build directories to ensure a reproducible output between each run.
Target "Clean" (fun _ -> CleanDir binaryDir)

// Generate the platform-specific Makefiles.
Target "Configure" (fun _ ->
ensureDirectory binaryDir
CMake.Generate (fun p ->
{ p with
Generator = if isUnix then "Unix Makefiles" else "Visual Studio 12 2013"
Variables =
if not isUnix then p.Variables else
[{Name = "CMAKE_BUILD_TYPE"; Value = CMakeString("Release")}]
}
)
)

// Build the binaries by calling the `cmake --build` wrapper.
Target "Build" (fun _ ->
CMake.Build (fun p ->
{ p with
Config = if isUnix then p.Config else "Release"
}
)
)

"Clean" ==> "Configure" ==> "Build"

RunTargetOrDefault "Build"
14 changes: 14 additions & 0 deletions Samples/CMakeSupport/Simple/main.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <cstdlib>
#include <iostream>

int main(int argc, char ** argv)
{
if (argc < 2)
{
std::cerr << "ERROR: Please provide at least one name as argument." << std::endl;
return EXIT_FAILURE;
}
for (int i = 1; i < argc; i++)
std::cout << "Hello " << argv[i] << "!" << std::endl;
return EXIT_SUCCESS;
}
21 changes: 21 additions & 0 deletions Samples/CMakeSupport/build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
:: Builds the CMake support examples on Windows.
@echo off

set "currentDir=%CD%"
set "baseDir=%~dp0"
set "fake=..\..\..\build\FAKE.exe"

cd "%baseDir%"
if %errorlevel% neq 0 exit /b %errorlevel%

cd Simple
"%fake%" .\build.fsx
if %errorlevel% neq 0 exit /b %errorlevel%

cd ..

cd Advanced
"%fake%" .\build.fsx
if %errorlevel% neq 0 exit /b %errorlevel%

cd "%currentDir%"
27 changes: 27 additions & 0 deletions Samples/CMakeSupport/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
# Builds the CMake support examples on Linux.
#
# To execute it on the Vagrant VM, simply execute the following steps:
# 1. Install the `cmake` and `build-essential` packages:
# sudo apt-get -y install cmake build-essential
# 2. Run this script from any directory:
# bash /vagrant/src/fake/Samples/CMakeSupport/build.sh

# Exit the script on any error.
set -e

currentDir="$PWD"
baseDir="$(dirname $0)"
fake="../../../build/FAKE.exe"

cd "$baseDir"

cd Simple
"$fake" ./build.fsx

cd ..

cd Advanced
"$fake" ./build.fsx

cd "$currentDir"