Skip to content

Commit 69a66d2

Browse files
committed
msbuild wip
1 parent 146b208 commit 69a66d2

File tree

15 files changed

+456
-1
lines changed

15 files changed

+456
-1
lines changed

.bazelci/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ tasks:
267267
- "//..."
268268
# The min supported version of rules_rust is `>=7`
269269
- "-//rust/..."
270+
# 6.5.0 seem to have a bug that does not honor target_compatible_with, so the msbuild target is getting triggered.
271+
- "-//msbuild_simple/..."
270272
build_targets: *min_supported_targets
271273
test_targets: *min_supported_targets
272274

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_test")
2+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "msbuild")
3+
4+
msbuild(
5+
name = "mylib",
6+
args = ["-p:Configuration=Release"],
7+
lib_source = "//msbuild_simple/code:srcs",
8+
out_lib_dir = "",
9+
out_static_libs = ["mylib.lib"],
10+
sln_file = "mysolution.sln",
11+
target_compatible_with = ["@platforms//os:windows"],
12+
)
13+
14+
cc_test(
15+
name = "mylib_test",
16+
srcs = ["test_mylib.cpp"],
17+
target_compatible_with = ["@platforms//os:windows"],
18+
deps = [":mylib"],
19+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
filegroup(
2+
name = "srcs",
3+
srcs = [
4+
"mylib.cpp",
5+
"mylib.h",
6+
"mylib.vcxproj",
7+
"mysolution.sln",
8+
],
9+
visibility = ["//msbuild_simple:__subpackages__"],
10+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "mylib.h"
2+
3+
std::string hello_mylib(void) { return "Hello from MyLib!"; }
4+
5+
int add_numbers(int a, int b) { return a + b; }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef MYLIB_H_
2+
#define MYLIB_H_ (1)
3+
4+
#include <string>
5+
6+
std::string hello_mylib(void);
7+
int add_numbers(int a, int b);
8+
9+
#endif
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>17.0</VCProjectVersion>
23+
<ProjectGuid>{B2C3D4E5-F6A7-8901-BCDE-F01234567890}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>mylib</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>StaticLibrary</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v143</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>StaticLibrary</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>StaticLibrary</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v143</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>StaticLibrary</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
75+
<TargetName>mylib</TargetName>
76+
</PropertyGroup>
77+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
78+
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
79+
<TargetName>mylib</TargetName>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
82+
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
83+
<TargetName>mylib</TargetName>
84+
</PropertyGroup>
85+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
86+
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
87+
<TargetName>mylib</TargetName>
88+
</PropertyGroup>
89+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
90+
<ClCompile>
91+
<WarningLevel>Level3</WarningLevel>
92+
<SDLCheck>true</SDLCheck>
93+
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
94+
<ConformanceMode>true</ConformanceMode>
95+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
96+
</ClCompile>
97+
<Lib>
98+
<AdditionalDependencies></AdditionalDependencies>
99+
</Lib>
100+
</ItemDefinitionGroup>
101+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
102+
<ClCompile>
103+
<WarningLevel>Level3</WarningLevel>
104+
<FunctionLevelLinking>true</FunctionLevelLinking>
105+
<IntrinsicFunctions>true</IntrinsicFunctions>
106+
<SDLCheck>true</SDLCheck>
107+
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
108+
<ConformanceMode>true</ConformanceMode>
109+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
110+
</ClCompile>
111+
<Lib>
112+
<AdditionalDependencies></AdditionalDependencies>
113+
</Lib>
114+
</ItemDefinitionGroup>
115+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
116+
<ClCompile>
117+
<WarningLevel>Level3</WarningLevel>
118+
<SDLCheck>true</SDLCheck>
119+
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
120+
<ConformanceMode>true</ConformanceMode>
121+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
122+
</ClCompile>
123+
<Lib>
124+
<AdditionalDependencies></AdditionalDependencies>
125+
</Lib>
126+
</ItemDefinitionGroup>
127+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
128+
<ClCompile>
129+
<WarningLevel>Level3</WarningLevel>
130+
<FunctionLevelLinking>true</FunctionLevelLinking>
131+
<IntrinsicFunctions>true</IntrinsicFunctions>
132+
<SDLCheck>true</SDLCheck>
133+
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
134+
<ConformanceMode>true</ConformanceMode>
135+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
136+
</ClCompile>
137+
<Lib>
138+
<AdditionalDependencies></AdditionalDependencies>
139+
</Lib>
140+
</ItemDefinitionGroup>
141+
<ItemGroup>
142+
<ClInclude Include="mylib.h" />
143+
</ItemGroup>
144+
<ItemGroup>
145+
<ClCompile Include="mylib.cpp" />
146+
</ItemGroup>
147+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
148+
<ImportGroup Label="ExtensionTargets">
149+
</ImportGroup>
150+
<Target Name="CopyHeaders" AfterTargets="Build">
151+
<MakeDir Directories="$(OutDir)include" />
152+
<Copy SourceFiles="mylib.h" DestinationFolder="$(OutDir)include" />
153+
</Target>
154+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.31903.59
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mylib", "mylib.vcxproj", "{B2C3D4E5-F6A7-8901-BCDE-F01234567890}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|x64 = Debug|x64
10+
Debug|Win32 = Debug|Win32
11+
Release|x64 = Release|x64
12+
Release|Win32 = Release|Win32
13+
EndGlobalSection
14+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
15+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|x64.ActiveCfg = Debug|x64
16+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|x64.Build.0 = Debug|x64
17+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|Win32.ActiveCfg = Debug|Win32
18+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Debug|Win32.Build.0 = Debug|Win32
19+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|x64.ActiveCfg = Release|x64
20+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|x64.Build.0 = Release|x64
21+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|Win32.ActiveCfg = Release|Win32
22+
{B2C3D4E5-F6A7-8901-BCDE-F01234567890}.Release|Win32.Build.0 = Release|Win32
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
GlobalSection(ExtensibilityGlobals) = postSolution
28+
SolutionGuid = {B5C6D7E8-F9A0-1234-5678-90ABCDEF0123} <!-- Another unique GUID -->
29+
EndGlobalSection
30+
EndGlobal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <stdexcept>
3+
#include <string>
4+
5+
#include "mylib.h"
6+
7+
int main(int argc, char* argv[]) {
8+
// Test the hello_mylib function
9+
std::string result = hello_mylib();
10+
if (result != "Hello from MyLib!") {
11+
throw std::runtime_error("Wrong result from hello_mylib: " + result);
12+
}
13+
14+
// Test the add_numbers function
15+
int math_result = add_numbers(5, 3);
16+
if (math_result != 8) {
17+
throw std::runtime_error("Wrong math_result from add_numbers: " +
18+
std::to_string(math_result));
19+
}
20+
21+
std::cout << "Everything's fine!";
22+
return 0;
23+
}

foreign_cc/defs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load(":cmake.bzl", _cmake = "cmake", _cmake_variant = "cmake_variant")
55
load(":configure.bzl", _configure_make = "configure_make", _configure_make_variant = "configure_make_variant")
66
load(":make.bzl", _make = "make", _make_variant = "make_variant")
77
load(":meson.bzl", _meson = "meson", _meson_with_requirements = "meson_with_requirements")
8+
load(":msbuild.bzl", _msbuild = "msbuild")
89
load(":ninja.bzl", _ninja = "ninja")
910
load(":utils.bzl", _runnable_binary = "runnable_binary")
1011

@@ -17,5 +18,6 @@ make_variant = _make_variant
1718
make = _make
1819
meson = _meson
1920
ninja = _ninja
21+
msbuild = _msbuild
2022
meson_with_requirements = _meson_with_requirements
2123
runnable_binary = _runnable_binary

foreign_cc/msbuild.bzl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""A rule for building projects using MSBuild"""
2+
3+
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
4+
load("//foreign_cc/private:cc_toolchain_util.bzl", "get_flags_info")
5+
load("//foreign_cc/private:detect_root.bzl", "detect_root")
6+
load(
7+
"//foreign_cc/private:framework.bzl",
8+
"CC_EXTERNAL_RULE_ATTRIBUTES",
9+
"CC_EXTERNAL_RULE_FRAGMENTS",
10+
"cc_external_rule_impl",
11+
"create_attrs",
12+
"expand_locations_and_make_variables",
13+
)
14+
load("//foreign_cc/private:msbuild_script.bzl", "create_msbuild_script")
15+
load("//toolchains/native_tools:tool_access.bzl", "get_msbuild_data")
16+
17+
def _msbuild(ctx):
18+
msbuild_data = get_msbuild_data(ctx)
19+
20+
tools_data = [msbuild_data]
21+
22+
attrs = create_attrs(
23+
ctx.attr,
24+
configure_name = "MSBuild",
25+
create_configure_script = _create_msbuild_script,
26+
tools_data = tools_data,
27+
msbuild_path = msbuild_data.path,
28+
)
29+
30+
return cc_external_rule_impl(ctx, attrs)
31+
32+
def _create_msbuild_script(configureParameters):
33+
ctx = configureParameters.ctx
34+
attrs = configureParameters.attrs
35+
inputs = configureParameters.inputs
36+
37+
root = detect_root(attrs.lib_source)
38+
flags = get_flags_info(ctx)
39+
40+
data = attrs.data + attrs.build_data
41+
42+
default_args = [
43+
"-p:TrackFileAccess=false",
44+
"-p:ForceImportAfterCppTargets=$$BUILD_TMPDIR/msbuild.props",
45+
"-p:OutDir=$$INSTALLDIR/",
46+
]
47+
48+
args = " ".join([
49+
expand_locations_and_make_variables(ctx, arg, "args", data)
50+
for arg in ctx.attr.args + default_args
51+
])
52+
53+
return create_msbuild_script(
54+
flags = flags,
55+
root = root,
56+
msbuild_path = attrs.msbuild_path,
57+
msbuild_sln_path = ctx.attr.sln_file,
58+
msbuild_args = args,
59+
include_dirs = inputs.include_dirs,
60+
ext_build_dirs = inputs.ext_build_dirs,
61+
)
62+
63+
def _attrs():
64+
attrs = dict(CC_EXTERNAL_RULE_ATTRIBUTES)
65+
attrs.update({
66+
"args": attr.string_list(mandatory = False, default = []),
67+
"sln_file": attr.string(mandatory = True),
68+
})
69+
return attrs
70+
71+
msbuild = rule(
72+
doc = "Rule for building external library with MSBuild.",
73+
attrs = _attrs(),
74+
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
75+
output_to_genfiles = True,
76+
provides = [CcInfo],
77+
implementation = _msbuild,
78+
toolchains = [
79+
"@rules_foreign_cc//toolchains:msbuild_toolchain",
80+
"@rules_foreign_cc//foreign_cc/private/framework:shell_toolchain",
81+
"@bazel_tools//tools/cpp:toolchain_type",
82+
],
83+
)

0 commit comments

Comments
 (0)