Skip to content
Open
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
2 changes: 2 additions & 0 deletions .bazelci/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide a link?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to pinpoint an issue or PR. However, using a later version of bazel solves this problem.

The error was that the target under //msbuild_simple/... what had a target_compatible_with for windows was still being executed on linux.

- "-//msbuild_simple/..."
build_targets: *min_supported_targets
test_targets: *min_supported_targets

Expand Down
19 changes: 19 additions & 0 deletions examples/msbuild_simple/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "msbuild")

msbuild(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I build this on my local I see

mylib.lib(mylib.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance

but /GL is not coming from the default msvc toolchain, it seems to be populated by msbuiild itself. Which is strange that it would actually say to add a different flag to the one it already added :)

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"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't it only ever be this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is true for MSVC msbuild, which is the one we adding. However, rule() doesn't allow you to set default target_compatible_with. It does support exec_compatible_with which also applies in this case. I've added that instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems crazy to have to set this every time you use the rule. Could we use a macro wrapper?

targets = ["Build"],
)

cc_test(
name = "mylib_test",
srcs = ["test_mylib.cpp"],
target_compatible_with = ["@platforms//os:windows"],
deps = [":mylib"],
)
10 changes: 10 additions & 0 deletions examples/msbuild_simple/code/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
filegroup(
name = "srcs",
srcs = [
"mylib.cpp",
"mylib.h",
"mylib.vcxproj",
"mysolution.sln",
],
visibility = ["//msbuild_simple:__subpackages__"],
)
5 changes: 5 additions & 0 deletions examples/msbuild_simple/code/mylib.cpp
Original file line number Diff line number Diff line change
@@ -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; }
9 changes: 9 additions & 0 deletions examples/msbuild_simple/code/mylib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef MYLIB_H_
#define MYLIB_H_ (1)

#include <string>

std::string hello_mylib(void);
int add_numbers(int a, int b);

#endif
154 changes: 154 additions & 0 deletions examples/msbuild_simple/code/mylib.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<ProjectGuid>{B2C3D4E5-F6A7-8901-BCDE-F01234567890}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>mylib</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>mylib</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>mylib</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>mylib</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>mylib</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<Lib>
<AdditionalDependencies></AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<Lib>
<AdditionalDependencies></AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<Lib>
<AdditionalDependencies></AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<Lib>
<AdditionalDependencies></AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="mylib.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="mylib.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Target Name="CopyHeaders" AfterTargets="Build">
<MakeDir Directories="$(OutDir)include" />
<Copy SourceFiles="mylib.h" DestinationFolder="$(OutDir)include" />
</Target>
</Project>
30 changes: 30 additions & 0 deletions examples/msbuild_simple/code/mysolution.sln
Original file line number Diff line number Diff line change
@@ -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} <!-- Another unique GUID -->
EndGlobalSection
EndGlobal
23 changes: 23 additions & 0 deletions examples/msbuild_simple/test_mylib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include <stdexcept>
#include <string>

#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;
}
2 changes: 2 additions & 0 deletions foreign_cc/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Loading