Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion buildcc/buildcc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Niket Naidu. All rights reserved.
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,12 +37,14 @@
// Specialized Toolchain
#include "toolchains/toolchain_gcc.h"
#include "toolchains/toolchain_msvc.h"
#include "toolchains/toolchain_mingw.h"

// TODO, Add more specialized toolchains here

// Specialized Targets
#include "targets/target_gcc.h"
#include "targets/target_msvc.h"
#include "targets/target_mingw.h"
#include "targets/target_generic.h"
#include "targets/target_custom.h"

Expand Down
1 change: 1 addition & 0 deletions buildcc/targets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(TARGET_SPECIALIZED_SRCS
# Specialized targets
include/targets/target_gcc.h
include/targets/target_msvc.h
include/targets/target_mingw.h

# User targets
include/targets/target_generic.h
Expand Down
87 changes: 87 additions & 0 deletions buildcc/targets/include/targets/target_mingw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TARGETS_TARGET_MINGW_H_
#define TARGETS_TARGET_MINGW_H_

#include "target/target.h"

#include "target_config_interface.h"
#include "target_gcc.h"

// MinGW
// ".exe", ".a", ".so" -> (x86_64-w64-mingw32)
namespace buildcc {

// Extensions
constexpr const char *const kMingwExecutableExt = ".exe";
constexpr const char *const kMingwDynamicLibExt = ".dll";

constexpr const char *const kMingwDynamicLibLinkCommand =
"{cpp_compiler} -shared {link_flags} {compiled_sources} -o {output} "
"-Wl,--out-implib,{output}.a";

class MingwConfig : ConfigInterface<MingwConfig> {
public:
static TargetConfig Executable() {
return DefaultMingwConfig(kMingwExecutableExt, kGccGenericCompileCommand,
kGccExecutableLinkCommand);
}

static TargetConfig DynamicLib() {
return DefaultMingwConfig(kMingwDynamicLibExt, kGccGenericCompileCommand,
kMingwDynamicLibLinkCommand);
}

private:
static TargetConfig DefaultMingwConfig(const std::string &target_ext,
const std::string &compile_command,
const std::string &link_command) {
TargetConfig config;
config.target_ext = target_ext;
config.obj_ext = kGccObjExt;
config.pch_header_ext = kGccPchHeaderExt;
config.pch_compile_ext = kGccPchCompileExt;
std::string prefix_include_dir = kGccPrefixIncludeDir;
std::string prefix_lib_dir = kGccPrefixLibDir;
config.pch_command = kGccGenericPchCompileCommand;
config.compile_command = compile_command;
config.link_command = link_command;
return config;
}
};

class ExecutableTarget_mingw : public ExecutableTarget_gcc {
public:
ExecutableTarget_mingw(const std::string &name,
const BaseToolchain &toolchain, const TargetEnv &env,
const TargetConfig &config = MingwConfig::Executable())
: ExecutableTarget_gcc(name, toolchain, env, config) {}
};

typedef StaticTarget_gcc StaticTarget_mingw;

class DynamicTarget_mingw : public DynamicTarget_gcc {
public:
DynamicTarget_mingw(const std::string &name, const BaseToolchain &toolchain,
const TargetEnv &env,
const TargetConfig &config = MingwConfig::DynamicLib())
: DynamicTarget_gcc(name, toolchain, env, config) {}
};

} // namespace buildcc

#endif
1 change: 1 addition & 0 deletions buildcc/toolchains/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(TOOLCHAIN_SPECIALIZED_SRCS
include/toolchains/toolchain_gcc.h
include/toolchains/toolchain_msvc.h
include/toolchains/toolchain_mingw.h
)

if(${BUILDCC_BUILD_AS_SINGLE_LIB})
Expand Down
33 changes: 33 additions & 0 deletions buildcc/toolchains/include/toolchains/toolchain_mingw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TOOLCHAINS_TOOLCHAIN_MINGW_H_
#define TOOLCHAINS_TOOLCHAIN_MINGW_H_

#include "toolchain/toolchain.h"

namespace buildcc {

class Toolchain_mingw : public BaseToolchain {
public:
Toolchain_mingw()
: Toolchain(ToolchainId::MinGW, "gcc", "as", "gcc", "g++", "ar", "ld") {}
Toolchain_mingw(const Toolchain_mingw &) = delete;
};

} // namespace buildcc

#endif
4 changes: 3 additions & 1 deletion docs/source/arch/style_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Google Style

``[snake_case_variable_name]_`` for private member variables

``k[PascalCaseVariableName]`` constexpr variables and enum / enum classes names
``k[PascalCaseVariableName]`` constexpr variables and enum / enum classes names ONLY when used internally

``[PascakCaseVariableName]`` constexpr variables and enum / enum classes names when exposed to users

.. attention:: If you see any wrong usage in the project please raise an issue
169 changes: 169 additions & 0 deletions docs/source/examples/mingw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,172 @@ MinGW
=======

Lowlevel MinGW Tests


Executable
-----------

.. code-block:: cpp
:linenos:
:emphasize-lines: 2,6

// MINGW specialized Toolchain
Toolchain_mingw mingw;

// MINGW specialized targets
// Creates `Simple.exe`
ExecutableTarget_mingw exetarget("Simple", mingw, "");
exetarget.GlobSources("src");
exetarget.AddIncludeDir("include", true);
exetarget.Build();

// Build
tf::Executor executor;
executor.run(exetarget.GetTaskflow());
executor.wait_for_all();

StaticLib
----------

.. code-block:: cpp
:linenos:
:emphasize-lines: 2,6,13

// MINGW specialized Toolchain
Toolchain_mingw mingw;

// MINGW specialized targets
// Creates `librandom.lib`
StaticTarget_mingw statictarget("librandom", mingw, "");
statictarget.AddSource("src/random.cpp");
statictarget.AddIncludeDir("include", true);
statictarget.Build();

// MINGW specialized targets
// Creates `Simple.exe`
ExecutableTarget_mingw exetarget("Simple", mingw, "");
exetarget.AddSource("src/main.cpp");
exetarget.AddIncludeDir("include", true);
exetarget.AddLibDep(statictarget);
exetarget.Build();

// Build
tf::Executor executor;
tf::Taskflow taskflow;

// Setup your dependencies explicitly
// Here statictarget needs to be built before exetarget
auto statictargetTask = taskflow.composed_of(statictarget.GetTaskflow());
auto exetargetTask = taskflow.composed_of(exetarget.GetTaskflow());
exetargetTask.succeed(statictargetTask);

executor.run(taskflow);
executor.wait_for_all();

DynamicLib
-----------

.. code-block:: cpp
:linenos:
:emphasize-lines: 2,6,13

// MINGW specialized Toolchain
Toolchain_mingw mingw;

// MINGW specialized targets
// Creates `librandom.lib` and `librandom.lib.dll`
DynamicTarget_mingw dynamictarget("librandom", mingw, "");
dynamictarget.AddSource("src/random.cpp");
dynamictarget.AddIncludeDir("include", true);
dynamictarget.Build();

// MINGW specialized target
// Creates `Simple.exe` which uses the above dynamic library
ExecutableTarget_mingw exetarget("Simple", mingw, "");
exetarget.AddSource("src/main.cpp");
exetarget.AddIncludeDir("include", true);
exetarget.AddLibDep(dynamictarget);
exetarget.Build();

// Build
tf::Executor executor;
tf::Taskflow taskflow;

// Setup your dependencies explicitly
// Here dynamictarget needs to be built before exetarget
auto dynamictargetTask = taskflow.composed_of(dynamictarget.GetTaskflow());
auto exetargetTask = taskflow.composed_of(exetarget.GetTaskflow());
exetargetTask.succeed(dynamictargetTask);

executor.run(taskflow);
executor.wait_for_all();

// Now that both your targets are built, copy the dynamictarget DLL to the exetarget location
// This is required for your exetarget to run properly
if (exetarget.IsBuilt()) {
fs::path copy_to_path =
exetarget.GetTargetBuildDir() / dynamictarget.GetTargetPath().filename();
fs::remove_all(copy_to_path);
fs::copy(dynamictarget.GetTargetPath(), copy_to_path);
}


.. note:: Our ``ExecutableTarget_mingw`` depends on ``DynamicTarget_mingw`` and requires the ``librandom.dll`` file to be present in the same folder location as the executable when running.

PrecompileHeader
-------------------

.. code-block:: cpp
:linenos:
:emphasize-lines: 2,4,7,24,25,26,39,40,41

int main() {
Toolchain_mingw mingw;

ExecutableTarget_mingw g_cppflags("cppflags", mingw, "files");
cppflags_build_cb(g_cppflags);

ExecutableTarget_mingw g_cflags("cflags", mingw, "files");
cflags_build_cb(g_cflags);

tf::Executor executor;
tf::Taskflow taskflow;

taskflow.composed_of(g_cppflags.GetTaskflow());
taskflow.composed_of(g_cflags.GetTaskflow());
executor.run(taskflow);
executor.wait_for_all();
}

static void cppflags_build_cb(BaseTarget &cppflags) {
cppflags.AddSource("main.cpp", "src");
cppflags.AddSource("random.cpp", "src");
cppflags.AddIncludeDir("include", true);

cppflags.AddPch("pch/pch_cpp.h");
cppflags.AddPch("pch/pch_c.h");
cppflags.AddIncludeDir("pch", true);

cppflags.AddPreprocessorFlag("-DRANDOM=1");
cppflags.AddCppCompileFlag("-Wall");
cppflags.AddCppCompileFlag("-Werror");
cppflags.AddLinkFlag("-lm");

cppflags.Build();
}

static void cflags_build_cb(BaseTarget &cflags) {
cflags.AddSource("main.c", "src");

cflags.AddPch("pch/pch_c.h");
cflags.AddIncludeDir("pch", false);
cflags.AddHeader("pch/pch_c.h");

cflags.AddPreprocessorFlag("-DRANDOM=1");
cflags.AddCCompileFlag("-Wall");
cflags.AddCCompileFlag("-Werror");
cflags.AddLinkFlag("-lm");

cflags.Build();
}

12 changes: 8 additions & 4 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ Multi hosts but only one target compiler used

> NOTE, See the distinction between **HOST** and **TARGET**

## [host] MinGW GCC/GNU -> [target] gcc
## [host] MSVC -> [target] gcc
## [host] Linux GCC/GNU -> [target] gcc
## [target] Gcc

- [x] Simple
- Only 1 source file
Expand All @@ -84,7 +82,7 @@ Multi hosts but only one target compiler used
- [ ] ClangFormat
- [ ] Taskflow graph visualizer

## [host] MSVC -> [target] MSVC
## [target] MSVC

> TODO, Understand how MSVC compilation using `cl.exe` occurs

Expand All @@ -95,6 +93,12 @@ Multi hosts but only one target compiler used
- [x] DynamicLib
- MSVC DynamicLib + Executable

## [target] MinGW

- [x] Executable
- [x] StaticLib
- [x] DynamicLib

# Real world Tests (Hybrid)

Multi hosts and multi targets
Expand Down
8 changes: 8 additions & 0 deletions example/mingw/DynamicLib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Folder
generated
buildcc

# Files
*.exe
*.o
*.bin
Loading