GCC#

Lowlevel GCC Tests

Simple#

Compile a single source

 1// GCC specialized toolchain
 2Toolchain_gcc toolchain;
 3
 4// GCC specialized targets
 5// Create "Simple" target (meant to use the GCC compiler)
 6// On Windows the equivalent is the MinGW compiler
 7ExecutableTarget_gcc target("Simple", gcc, "");
 8target.AddSource("main.cpp");
 9target.Build();
10
11// Build
12tf::Executor executor;
13executor.run(target.GetTaskflow());
14executor.wait_for_all();

IncludeDir#

Compile multiple sources with header files

 1// GCC specialized toolchain
 2Toolchain_gcc toolchain;
 3
 4// GCC specialized targets
 5// Create "IncludeDir" target (meant to use the GCC compiler)
 6// On Windows the equivalent is the MinGW compiler
 7ExecutableTarget_gcc target("IncludeDir", gcc, "files");
 8target.AddSource("main.cpp", "src");
 9target.AddSource("src/random.cpp");
10
11// Track header for rebuilds
12target.AddHeader("include/random.h");
13
14// Add include dir to search paths
15target.AddIncludeDir("include");
16target.Build();
17
18// Build
19tf::Executor executor;
20executor.run(target.GetTaskflow());
21executor.wait_for_all();

StaticLib#

Compile a static library which is used by an executable

 1// GCC specialized toolchain
 2Toolchain_gcc toolchain;
 3
 4// GCC specialized targets
 5// Create "librandom.a" target (meant to use the GCC compiler)
 6// On Windows the equivalent is the MinGW compiler
 7StaticTarget_gcc statictarget("librandom", gcc, "files");
 8statictarget.AddSource("src/random.cpp");
 9statictarget.AddHeader("include/random.h");
10statictarget.AddIncludeDir("include");
11statictarget.Build();
12
13// GCC specialized targets
14// Create "statictest" target (meant to use the GCC compiler)
15// On Windows the equivalent is the MinGW compiler
16ExecutableTarget_gcc exetarget("statictest", gcc, "files");
17exetarget.AddSource("main.cpp", "src");
18exetarget.AddIncludeDir("include");
19exetarget.AddLibDep(statictarget);
20exetarget.Build();
21
22// Build
23tf::Executor executor;
24tf::Taskflow taskflow;
25
26// Explicitly setup your dependencies
27tf::Task statictargetTask = taskflow.composed_of(statictarget.GetTaskflow());
28tf::Task exetargetTask = taskflow.composed_of(exetarget.GetTaskflow());
29exetargetTask.succeed(statictargetTask);
30
31// Run
32executor.run(taskflow);
33executor.wait_for_all();

DynamicLib#

Compile a dynamic library which is used by an executable

 1// GCC specialized toolchain
 2Toolchain_gcc toolchain;
 3
 4// GCC specialized targets
 5// Create "librandom.so" target (meant to use the GCC compiler)
 6// On Windows the equivalent is the MinGW compiler
 7DynamicTarget_gcc dynamictarget("librandom", gcc, "files");
 8dynamictarget.AddSource("src/random.cpp");
 9dynamictarget.AddHeader("include/random.h");
10dynamictarget.AddIncludeDir("include");
11dynamictarget.Build();
12
13// GCC specialized targets
14// Create "dynamictest" target (meant to use the GCC compiler)
15// On Windows the equivalent is the MinGW compiler
16ExecutableTarget_gcc target("dynamictest", gcc, "files");
17target.AddSource("main.cpp", "src");
18target.AddIncludeDir("include");
19target.AddLibDep(dynamictarget);
20target.Build();
21
22// Build
23tf::Executor executor;
24tf::Taskflow taskflow;
25
26// Explicitly setup your dependencies
27auto dynamictargetTask = taskflow.composed_of(dynamictarget.GetTaskflow());
28auto targetTask = taskflow.composed_of(target.GetTaskflow());
29targetTask.succeed(dynamictargetTask);
30
31executor.run(taskflow);
32executor.wait_for_all();
33
34// Post Build step
35if (target.IsBuilt()) {
36    fs::path copy_to_path =
37        target.GetTargetBuildDir() / dynamictarget.GetTargetPath().filename();
38    fs::copy(dynamictarget.GetTargetPath(), copy_to_path);
39}

Note

Our ExecutableTarget_gcc depends on DynamicTarget_gcc and requires the librandom.so file to be present in the same folder location as the executable when running.

Flags#

Using PreprocessorFlags, C Compile flags, Cpp Compile flags and Link flags

 1// GCC specialized toolchain
 2Toolchain_gcc toolchain;
 3
 4// GCC specialized targets
 5// Create "CppFlags" target (meant to use the GCC compiler)
 6// On Windows the equivalent is the MinGW compiler
 7ExecutableTarget_gcc cpptarget("CppFlags", gcc, "files");
 8cpptarget.AddSource("main.cpp", "src");
 9cpptarget.AddSource("src/random.cpp");
10cpptarget.AddHeader("include/random.h");
11cpptarget.AddIncludeDir("include");
12cpptarget.AddPreprocessorFlag("-DRANDOM=1");
13cpptarget.AddCppCompileFlag("-Wall");
14cpptarget.AddCppCompileFlag("-Werror");
15cpptarget.AddLinkFlag("-lm");
16cpptarget.Build();
17
18// Gcc specialized targets
19// Create "CFlags" target (meant to use the GCC compiler)
20// On Windows the equivalent is the MinGW compiler
21ExecutableTarget_gcc ctarget("CFlags", gcc, "files");
22ctarget.AddSource("main.c", "src");
23ctarget.AddPreprocessorFlag("-DRANDOM=1");
24ctarget.AddCCompileFlag("-Wall");
25ctarget.AddCCompileFlag("-Werror");
26ctarget.AddLinkFlag("-lm");
27ctarget.Build();
28
29// Build
30tf::Executor executor;
31tf::Taskflow taskflow;
32
33// There isn't any dependency between the 2 targets
34taskflow.composed_of(cpptarget.GetTaskflow());
35taskflow.composed_of(ctarget.GetTaskflow());
36
37executor.run(taskflow);
38executor.wait_for_all();

AfterInstall#

Use BuildCC with CMake

  • Install BuildCC via CMake to your system and add it to PATH

  • Use the script below to compile your build script to an executable

  • Copy the Flags build example

  • Run the executable from your project root directory

# Package dependencies
# fmt is imported by spdlog by default
find_package(fmt_package NAMES "fmt" REQUIRED)
find_package(spdlog_package NAMES "spdlog" REQUIRED)
find_package(nlohmann_json_package NAMES "nlohmann_json" REQUIRED)
find_package(taskflow_package NAMES "Taskflow" "taskflow" REQUIRED)
find_package(CLI11_package NAMES "CLI11" REQUIRED)
find_package(tiny_process_library_package NAMES "tiny-process-library" REQUIRED)

find_package(buildcc_package NAMES "buildcc" REQUIRED)

message("Find package: ${fmt_package_DIR}")
message("Find package: ${spdlog_package_DIR}")
message("Find package: ${nlohmann_json_package_DIR}")
message("Find package: ${taskflow_package_DIR}")
message("Find package: ${CLI11_package_DIR}")
message("Find package: ${tiny_process_library_package_DIR}") #

message("Find package: ${buildcc_package_DIR}")

# build executable
add_executable(build build.cpp)
target_include_directories(build PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated)
target_link_libraries(build PRIVATE
    buildcc
)
if (${MINGW})
    message(WARNING "-Wl,--allow-multiple-definition for MINGW")
    target_link_options(build PRIVATE -Wl,--allow-multiple-definition)
endif()

# Add your constants file for the environment
configure_file(constants.h.in ${CMAKE_BINARY_DIR}/generated/constants.h @ONLY)

Plugins#

Demonstrating BuildCC supported plugin usage

1plugin::ClangCompileCommands({&cppflags, &cflags}).Generate();

PrecompileHeader#

TODO