Skip to content

Commit

Permalink
Fea,添加Linux交叉编译支持
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed May 15, 2023
1 parent 50988ee commit ab1d23a
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 43 deletions.
71 changes: 43 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,51 @@ msbuild "D:\ConsoleApplication2\ConsoleApplication2.vcxproj" -p:Configuration=Re
msbuild "D:\ConsoleApplication2\ConsoleApplication2.vcxproj" -p:Configuration=Release;Platform=x64
```
## 2.4. 全局支持的属性
### 2.4.1. PlatformToolset 全局属性
工具集,目前支持`YY_Cross_Clang_1_0``YY_Cross_GCC_1_0`

示例:
```xml
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
<PlatformToolset>YY_Cross_GCC_1_0</PlatformToolset>
</PropertyGroup>
```
### 2.4.2. Platform 全局属性
一般来说,Platform拥有以下几种可能:
* ARM
* ARM64
* MIPS
* x64
* x86

> 注意:Platform是在调用msbuild时传入的,一般不应该直接设置到工程。
### 2.4.3. PlatformTriplet 全局属性
平台的Triplet值,比如说:i686-linux-gnu。一般无需设置,框架会根据Platform属性自动调整。
其默认对照关系如下:

| Platform | Linux PlatformTriplet 默认值 | MacOS PlatformTriplet 默认值 |
| --------- | ---------------------------- | ---------------------------- |
| x86 | i686-linux-gnu | 尚未就绪
| x64 | x86_64-linux-gnu | 尚未就绪
| ARM | arm-linux-gnueabihf | 尚未就绪
| ARM64 | aarch64-linux-gnu | 尚未就绪
| MIPS | mips-linux-gnu | 不支持

示例:
```xml
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
<PlatformTriplet>i686-linux-gnu</PlatformTriplet>
</PropertyGroup>
```

### 2.4.4. Sysroot 全局属性
用于自定义库目录以及头文件位置。此路径会通过`--sysroot`传递给编译器以及链接器。


# 3. 支持的属性以及元素参数映射情况

## 3.1. ClCompile
它描述了C/C++代码编译参数配置情况。

Expand Down Expand Up @@ -519,20 +562,6 @@ ObjCAutomaticRefCounting开启时发生异常保证不泄露内存。
</ClCompile>
```

### 3.1.31. Sysroot 属性(字符串)
设置库和头文件的根目录,一般交叉编译时需要。

| Windows(MSVC) | Linux(GCC/CLang) | OSX(GCC/CLang)
| ----------------- | ----------------- | --------
| 自动忽略 | --sysroot | --sysroot

示例:
```xml
<ClCompile>
<Sysroot>Usr/aarch64-linux-gnu</Sysroot>
</ClCompile>
```

## 3.2. Link
链接配置。

Expand Down Expand Up @@ -812,20 +841,6 @@ Apple特有的Framework引用(-framework)。
</Link>
```

### 3.1.22. Sysroot 属性(字符串)
设置库和头文件的根目录,一般交叉编译时需要。

| Windows(MSVC) | Linux(GCC/CLang) | OSX(GCC/CLang)
| ----------------- | ----------------- | --------
| 自动忽略 | --sysroot | --sysroot

示例:
```xml
<Link>
<Sysroot>Usr/aarch64-linux-gnu</Sysroot>
</Link>
```

# 附: 我怎么自己编译 MSBuildCppCrossToolset?
> 温馨提示:普通用户无需关心如何编译 MSBuildCppCrossToolset。只需要从Release产物中下载即可。
Expand Down
24 changes: 18 additions & 6 deletions Samples/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ export VCTargetsPath=/home/mouri/Workspace/MSBuildCppCrossToolsetRelease/VCTarge

### 编译项目
一般来说,Platform拥有以下几种可能:
* ARM
* ARM64
* MIPS
* x64
* x86(注意:对于Windows系统下的vcxproj来说叫Win32)

> 温馨提示:Windows下搞不懂x86还是Win32的可以先尝试Win32,如果报错那么尝试一下x86。
| Platform | Linux PlatformTriplet 默认值 | MacOS PlatformTriplet 默认值 | 备注
| --------- | ---------------------------- | --------------------- | ---
| x86 | i686-linux-gnu | 尚未就绪 | (注意:对于Windows系统下的vcxproj来说叫Win32)
| x64 | x86_64-linux-gnu | 尚未就绪
| ARM | arm-linux-gnueabihf | 尚未就绪
| ARM64 | aarch64-linux-gnu | 尚未就绪
| MIPS | mips-linux-gnu | 不支持

> 温馨提示:Windows下Platform搞不懂x86还是Win32的可以先尝试Win32,如果报错那么尝试一下x86。
如果对默认的PlatformTriplet不满意,可以在Configuration区域覆盖PlatformTriplet属性实现自定义。Configuration区域亦可自定义Sysroot属性。


交叉编译时,必须安装对应的Triplet,比如对于一个x64的Ubuntu,使用GCC编译ARM64小端的Linux程序,则先安装`g++-aarch64-linux-gun`

```
sudo apt-get install g++-aarch64-linux-gun
```

; 假设项目位置为 `/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples`。并且编译一些 Release,那么可以输入如下命令:

Expand Down
28 changes: 28 additions & 0 deletions YY.Build.Cross.Tasks/Cross/Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Compile()
switchOrderList.Add("ForcedIncludeFiles");
switchOrderList.Add("EnableASAN");
switchOrderList.Add("AdditionalOptions");
switchOrderList.Add("TargetArch");
switchOrderList.Add("Sysroot");
switchOrderList.Add("DependenceFile");

Expand Down Expand Up @@ -992,6 +993,33 @@ public virtual string Sysroot
}
}

// 例如 x86_64-linux-gnu
// 注意:仅clang支持,并且此选项位内部设置,跟随PlatformTriplet
public virtual string TargetArch
{
get
{
if (IsPropertySet("TargetArch"))
{
return base.ActiveToolSwitches["TargetArch"].Value;
}
return null;
}
set
{
base.ActiveToolSwitches.Remove("TargetArch");
ToolSwitch toolSwitch = new ToolSwitch(ToolSwitchType.String);
toolSwitch.DisplayName = "目标体系结构";
toolSwitch.Description = "目标的体系结构";
toolSwitch.ArgumentRelationList = new ArrayList();
toolSwitch.Name = "TargetArch";
toolSwitch.Value = value;
toolSwitch.SwitchValue = "--target=";
base.ActiveToolSwitches.Add("TargetArch", toolSwitch);
AddActiveSwitchToolValue(toolSwitch);
}
}

protected override bool GenerateCostomCommandsAccordingToType(CommandLineBuilder builder, string switchName, bool dummyForBackwardCompatibility, CommandLineFormat format = CommandLineFormat.ForBuildLog, EscapeFormat escapeFormat = EscapeFormat.Default)
{
if (string.Equals(switchName, "DependenceFile", StringComparison.OrdinalIgnoreCase))
Expand Down
28 changes: 28 additions & 0 deletions YY.Build.Cross.Tasks/Cross/Ld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public Ld()
switchOrderList.Add("Relocation");
switchOrderList.Add("FunctionBinding");
switchOrderList.Add("NoExecStackRequired");
switchOrderList.Add("TargetArch");
switchOrderList.Add("SysRoot");
switchOrderList.Add("LinkDll");
switchOrderList.Add("WholeArchiveBegin");
Expand Down Expand Up @@ -827,6 +828,33 @@ public virtual string SysRoot
}
}

// 例如 x86_64-linux-gnu
// 注意:仅clang支持,并且此选项位内部设置,跟随PlatformTriplet
public virtual string TargetArch
{
get
{
if (IsPropertySet("TargetArch"))
{
return base.ActiveToolSwitches["TargetArch"].Value;
}
return null;
}
set
{
base.ActiveToolSwitches.Remove("TargetArch");
ToolSwitch toolSwitch = new ToolSwitch(ToolSwitchType.String);
toolSwitch.DisplayName = "目标体系结构";
toolSwitch.Description = "目标的体系结构";
toolSwitch.ArgumentRelationList = new ArrayList();
toolSwitch.Name = "TargetArch";
toolSwitch.Value = value;
toolSwitch.SwitchValue = "--target=";
base.ActiveToolSwitches.Add("TargetArch", toolSwitch);
AddActiveSwitchToolValue(toolSwitch);
}
}

protected override string TrackerIntermediateDirectory
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<Import Project="$(VCTargetsPath)\Application Type\$(ApplicationType)\$(ApplicationTypeRevision)\YY.Linux.Cross.props" />

<PropertyGroup>
<Sysroot></Sysroot>
<ClangTarget></ClangTarget>
<ClangTarget>$(PlatformTriplet)</ClangTarget>
<ToolExe>clang++</ToolExe>
<RemoteCCompileToolExe Condition="'$(RemoteCCompileToolExe)' == ''">clang++</RemoteCCompileToolExe>
<RemoteCppCompileToolExe Condition="'$(RemoteCppCompileToolExe)' == ''">clang++</RemoteCppCompileToolExe>
<RemoteLdToolExe Condition="'$(RemoteLdToolExe)' == ''">clang++</RemoteLdToolExe>
<RemoteArToolExe Condition="'$(RemoteArToolExe)' == ''">ar</RemoteArToolExe>
<RemoteArToolExe Condition="'$(RemoteArToolExe)' == ''">$(PlatformTriplet)-ar</RemoteArToolExe>
<RemoteGdbserverToolExe Condition="'$(RemoteGdbserverToolExe)' == ''">gdbserver</RemoteGdbserverToolExe>
<RemoteGdbToolExe Condition="'$(RemoteGdbToolExe)' == ''">gdb</RemoteGdbToolExe>
<!-- Set $(ExecutablePath) used by Microsoft.Cpp.CurrentVersion.targets's SetBuildDefaultEnvironmentVariables target to set the PATH env var -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<Import Project="$(VCTargetsPath)\Application Type\$(ApplicationType)\$(ApplicationTypeRevision)\YY.Linux.Cross.props" />

<PropertyGroup>
<Sysroot></Sysroot>
<ClangTarget></ClangTarget>
<ToolExe>g++</ToolExe>
<RemoteCCompileToolExe Condition="'$(RemoteCCompileToolExe)' == ''">g++</RemoteCCompileToolExe>
<RemoteCppCompileToolExe Condition="'$(RemoteCppCompileToolExe)' == ''">g++</RemoteCppCompileToolExe>
<RemoteLdToolExe Condition="'$(RemoteLdToolExe)' == ''">g++</RemoteLdToolExe>
<RemoteArToolExe Condition="'$(RemoteArToolExe)' == ''">ar</RemoteArToolExe>
<RemoteCCompileToolExe Condition="'$(RemoteCCompileToolExe)' == ''">$(PlatformTriplet)-g++</RemoteCCompileToolExe>
<RemoteCppCompileToolExe Condition="'$(RemoteCppCompileToolExe)' == ''">$(PlatformTriplet)-g++</RemoteCppCompileToolExe>
<RemoteLdToolExe Condition="'$(RemoteLdToolExe)' == ''">$(PlatformTriplet)-g++</RemoteLdToolExe>
<RemoteArToolExe Condition="'$(RemoteArToolExe)' == ''">$(PlatformTriplet)-ar</RemoteArToolExe>
<RemoteGdbserverToolExe Condition="'$(RemoteGdbserverToolExe)' == ''">gdbserver</RemoteGdbserverToolExe>
<RemoteGdbToolExe Condition="'$(RemoteGdbToolExe)' == ''">gdb</RemoteGdbToolExe>
<!-- Set $(ExecutablePath) used by Microsoft.Cpp.CurrentVersion.targets's SetBuildDefaultEnvironmentVariables target to set the PATH env var -->
Expand Down
6 changes: 5 additions & 1 deletion YY.Build.Cross.Tasks/Targets/Linux/YY.Linux.Cross.targets
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<CompileAs Condition="'%(ClCompile.CompileAs)' == 'Default' and '%(ClCompile.Extension)' == '.c'">CompileAsC</CompileAs>
<WholeProgramOptimization Condition="'%(ClCompile.LinkTimeOptimization)' == 'true'">true</WholeProgramOptimization>
<FloatingPointModel Condition="'%(ClCompile.RelaxIEEE)' == 'true'">Fast</FloatingPointModel>
<Sysroot>$(Sysroot)</Sysroot>
<TargetArch>$(ClangTarget)</TargetArch>
</ClCompile>

<ClCompile>
Expand Down Expand Up @@ -155,6 +157,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
EnableASAN="%(ClCompile.EnableASAN)"
AdditionalOptions="%(ClCompile.AdditionalOptions)"
Sysroot="%(ClCompile.Sysroot)"
TargetArch="%(ClCompile.TargetArch)"
TrackerLogDirectory="$(TLogLocation)"
TLogReadFiles="$(TLogLocation)\compile.read.1.tlog"
TLogWriteFiles="$(TLogLocation)\compile.write.1.tlog"
Expand Down Expand Up @@ -201,7 +204,8 @@ Copyright (C) Microsoft Corporation. All rights reserved.
BuildingInIde="%(Link.BuildingInIde)"
EnableASAN="%(Link.EnableASAN)"
UseOfStl="%(Link.UseOfStl)"
Sysroot="%(ClCompile.Sysroot)"
Sysroot="$(Sysroot)"
TargetArch="$(ClangTarget)"
TrackerLogDirectory="$(TLogLocation)"
TLogReadFiles="$(TLogLocation)\link.read.1.tlog"
TLogWriteFiles="$(TLogLocation)\link.write.1.tlog"
Expand Down
2 changes: 2 additions & 0 deletions YY.Build.Cross.Tasks/YY.Build.Cross.Tasks.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<None Include="$(MSBuildThisFileDirectory)Targets\Linux\YY.Linux.Cross.targets" />
<None Include="$(MSBuildThisFileDirectory)Targets\OSX\Platforms\YY_Cross_Clang_1_0\Toolset.props" />
<None Include="$(MSBuildThisFileDirectory)Targets\OSX\Platforms\YY_Cross_Clang_1_0\Toolset.targets" />
<None Include="$(MSBuildThisFileDirectory)Targets\OSX\Platforms\YY_Cross_GCC_1_0\Toolset.props" />
<None Include="$(MSBuildThisFileDirectory)Targets\OSX\Platforms\YY_Cross_GCC_1_0\Toolset.targets" />
<None Include="$(MSBuildThisFileDirectory)Targets\OSX\YY.OSX.Cross.props" />
<None Include="$(MSBuildThisFileDirectory)Targets\OSX\YY.OSX.Cross.targets" />
</ItemGroup>
Expand Down

0 comments on commit ab1d23a

Please sign in to comment.