diff --git a/.bazelci/config.yaml b/.bazelci/config.yaml index 289015139..cc6102647 100644 --- a/.bazelci/config.yaml +++ b/.bazelci/config.yaml @@ -267,6 +267,8 @@ tasks: - "//..." # The min supported version of rules_rust is `>=7` - "-//rust/..." + # 6.5.0 seem to have a bug that does not honor target_compatible_with, so the msbuild target is getting triggered. + - "-//msbuild_simple/..." build_targets: *min_supported_targets test_targets: *min_supported_targets diff --git a/examples/msbuild_simple/BUILD.bazel b/examples/msbuild_simple/BUILD.bazel new file mode 100644 index 000000000..795158199 --- /dev/null +++ b/examples/msbuild_simple/BUILD.bazel @@ -0,0 +1,19 @@ +load("@rules_cc//cc:defs.bzl", "cc_test") +load("@rules_foreign_cc//foreign_cc:defs.bzl", "msbuild") + +msbuild( + name = "mylib", + lib_source = "//msbuild_simple/code:srcs", + out_lib_dir = "", + out_static_libs = ["mylib.lib"], + sln_file = "mysolution.sln", + target_compatible_with = ["@platforms//os:windows"], + targets = ["Build"], +) + +cc_test( + name = "mylib_test", + srcs = ["test_mylib.cpp"], + target_compatible_with = ["@platforms//os:windows"], + deps = [":mylib"], +) diff --git a/examples/msbuild_simple/code/BUILD.bazel b/examples/msbuild_simple/code/BUILD.bazel new file mode 100644 index 000000000..daf2149d4 --- /dev/null +++ b/examples/msbuild_simple/code/BUILD.bazel @@ -0,0 +1,10 @@ +filegroup( + name = "srcs", + srcs = [ + "mylib.cpp", + "mylib.h", + "mylib.vcxproj", + "mysolution.sln", + ], + visibility = ["//msbuild_simple:__subpackages__"], +) diff --git a/examples/msbuild_simple/code/mylib.cpp b/examples/msbuild_simple/code/mylib.cpp new file mode 100644 index 000000000..193efe107 --- /dev/null +++ b/examples/msbuild_simple/code/mylib.cpp @@ -0,0 +1,5 @@ +#include "mylib.h" + +std::string hello_mylib(void) { return "Hello from MyLib!"; } + +int add_numbers(int a, int b) { return a + b; } diff --git a/examples/msbuild_simple/code/mylib.h b/examples/msbuild_simple/code/mylib.h new file mode 100644 index 000000000..107e6aba5 --- /dev/null +++ b/examples/msbuild_simple/code/mylib.h @@ -0,0 +1,9 @@ +#ifndef MYLIB_H_ +#define MYLIB_H_ (1) + +#include + +std::string hello_mylib(void); +int add_numbers(int a, int b); + +#endif diff --git a/examples/msbuild_simple/code/mylib.vcxproj b/examples/msbuild_simple/code/mylib.vcxproj new file mode 100644 index 000000000..29a6256e6 --- /dev/null +++ b/examples/msbuild_simple/code/mylib.vcxproj @@ -0,0 +1,154 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890} + Win32Proj + mylib + 10.0 + + + + StaticLibrary + true + v143 + Unicode + + + StaticLibrary + false + v143 + true + Unicode + + + StaticLibrary + true + v143 + Unicode + + + StaticLibrary + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + $(ProjectDir)obj\$(Platform)\$(Configuration)\ + mylib + + + $(ProjectDir)obj\$(Platform)\$(Configuration)\ + mylib + + + $(ProjectDir)obj\$(Platform)\$(Configuration)\ + mylib + + + $(ProjectDir)obj\$(Platform)\$(Configuration)\ + mylib + + + + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + + + + + + + + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + + + + + + + + Level3 + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + + + + + + + + Level3 + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/msbuild_simple/code/mysolution.sln b/examples/msbuild_simple/code/mysolution.sln new file mode 100644 index 000000000..f4a61b4e5 --- /dev/null +++ b/examples/msbuild_simple/code/mysolution.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mylib", "mylib.vcxproj", "{B2C3D4E5-F6A7-8901-BCDE-F01234567890}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|Win32 = Debug|Win32 + Release|x64 = Release|x64 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|x64.ActiveCfg = Debug|x64 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|x64.Build.0 = Debug|x64 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|Win32.ActiveCfg = Debug|Win32 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|Win32.Build.0 = Debug|Win32 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|x64.ActiveCfg = Release|x64 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|x64.Build.0 = Release|x64 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|Win32.ActiveCfg = Release|Win32 + {B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B5C6D7E8-F9A0-1234-5678-90ABCDEF0123} + EndGlobalSection +EndGlobal diff --git a/examples/msbuild_simple/test_mylib.cpp b/examples/msbuild_simple/test_mylib.cpp new file mode 100644 index 000000000..9800a8ac9 --- /dev/null +++ b/examples/msbuild_simple/test_mylib.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +#include "mylib.h" + +int main(int argc, char* argv[]) { + // Test the hello_mylib function + std::string result = hello_mylib(); + if (result != "Hello from MyLib!") { + throw std::runtime_error("Wrong result from hello_mylib: " + result); + } + + // Test the add_numbers function + int math_result = add_numbers(5, 3); + if (math_result != 8) { + throw std::runtime_error("Wrong math_result from add_numbers: " + + std::to_string(math_result)); + } + + std::cout << "Everything's fine!"; + return 0; +} diff --git a/foreign_cc/defs.bzl b/foreign_cc/defs.bzl index 09f219722..fa28130a8 100644 --- a/foreign_cc/defs.bzl +++ b/foreign_cc/defs.bzl @@ -5,6 +5,7 @@ load(":cmake.bzl", _cmake = "cmake", _cmake_variant = "cmake_variant") load(":configure.bzl", _configure_make = "configure_make", _configure_make_variant = "configure_make_variant") load(":make.bzl", _make = "make", _make_variant = "make_variant") load(":meson.bzl", _meson = "meson", _meson_with_requirements = "meson_with_requirements") +load(":msbuild.bzl", _msbuild = "msbuild") load(":ninja.bzl", _ninja = "ninja") load(":utils.bzl", _runnable_binary = "runnable_binary") @@ -17,5 +18,6 @@ make_variant = _make_variant make = _make meson = _meson ninja = _ninja +msbuild = _msbuild meson_with_requirements = _meson_with_requirements runnable_binary = _runnable_binary diff --git a/foreign_cc/msbuild.bzl b/foreign_cc/msbuild.bzl new file mode 100644 index 000000000..97605cf3e --- /dev/null +++ b/foreign_cc/msbuild.bzl @@ -0,0 +1,202 @@ +"""# [MSBuild](#msbuild) + +This rule is tailored for MSBuild.exe from the Visual Studio installation. It does not support +the dotnet msbuild execution path. MSBuild determines it own compile and linker flags based +on the project/solution file configuration. This cannot be fully controlled from bazel like +other rules (e.g. cmake). We do our best by generating a `msbuild.props` file that is used by +specifying `-p:ForceImportAfterCppTargets=msbuild.props` to append bazel flags and override +existing flags where possible. + +Since MSBuild is closed source project from Microsoft, there is no prebuilt toolchain available +and we cannot build it from source. The default is a pre-installed toolchain which assumes +MSBuild.exe is installed as a part of Visual Studio and locateable by bazel. If you want to +implement your own MSBuild toolchain you will need to define a toolchain that implements the +toolchain type `@rules_foreign_cc//toolchains:msbuild_toolchain`. E.g. + +``` +toolchain( + name = "msbuild_toolchain", + exec_compatible_with = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], + target_compatible_with = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], + toolchain = ":_msbuild_toolchain", + toolchain_type = "@rules_foreign_cc//toolchains:msbuild_toolchain", +) + +native_tool_toolchain( + name = "_msbuild_toolchain", + path = "", + target = "