Skip to content

Commit

Permalink
Fea,MacOS添加交叉编译支持
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed May 16, 2023
1 parent ab1d23a commit 195e17b
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 92 deletions.
72 changes: 42 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,15 @@ export VCTargetsPath=/home/john/Desktop/VCTargets/v170/
## 2.3. 编译vcxproj项目
我们也提供了示例项目,点击查看[Samples](Samples)

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


假设项目位置: `/home/john/Desktop/ConsoleApplication2/ConsoleApplication2.vcxproj`。并且编译 Release版的x86版本,那么可以输入如下命令:

```
; Linux下编译 x86
dotnet msbuild '/home/john/Desktop/ConsoleApplication2/ConsoleApplication2.vcxproj' '-p:Configuration=Release;Platform=x86'
; Linux下编译 x64
; Linux、MacOS下编译 x64
dotnet msbuild '/home/john/Desktop/ConsoleApplication2/ConsoleApplication2.vcxproj' '-p:Configuration=Release;Platform=x64'
; Linux、MacOS下编译小端 ARM64
dotnet msbuild '/home/john/Desktop/ConsoleApplication2/ConsoleApplication2.vcxproj' '-p:Configuration=Release;Platform=ARM64'
; Windows下编译 x86。新人特别注意了,vcxproj里没有x86,只有Win32!!!
msbuild "D:\ConsoleApplication2\ConsoleApplication2.vcxproj" -p:Configuration=Release;Platform=Win32
Expand All @@ -77,46 +69,66 @@ msbuild "D:\ConsoleApplication2\ConsoleApplication2.vcxproj" -p:Configuration=Re
```
## 2.4. 全局支持的属性
### 2.4.1. PlatformToolset 全局属性
工具集,目前支持`YY_Cross_Clang_1_0``YY_Cross_GCC_1_0`
工具集,目前支持以下工具集:

| 工具集 | 说明
| ------------------ | -----------------
| YY_Cross_GCC_1_0 | GCC工具集,Linux系统默认的工具集,MacOS暂时对YY_Cross_GCC_1_0支持不好。
| YY_Cross_Clang_1_0 | CLang工具集,MacOS系统默认的工具集。

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

| 目标系统 | 支持的 Platform
| -------- | ---------
| Linux | x86、x64、ARM、ARM64、MIPS
| MacOS | x86、x64、ARM64

> 注意:Platform是在调用msbuild时传入的,一般不应该直接设置到工程。
示例:
```
dotnet msbuild '/home/john/Desktop/ConsoleApplication2/ConsoleApplication2.vcxproj' '-p:Configuration=Release;Platform=x64'
```

> 特别提醒:特别是Linux系统。编译时,必须安装Platform对应的Triplet,否则将找不到g++而失败!比如对于一个x64的Ubuntu,使用GCC编译ARM64小端的Linux程序,则先安装 `g++-aarch64-linux-gun`,命令如下所示:
```
sudo apt-get install g++-aarch64-linux-gun
```


### 2.4.3. PlatformTriplet 全局属性
平台的Triplet值,比如说:i686-linux-gnu。一般无需设置,框架会根据Platform属性自动调整。
其默认对照关系如下
各个平台Platform的PlatformTriplet默认值如下表所示

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

示例:
如果对默认的`PlatformTriplet`值不满意,可以考虑手工调整,示例:
```xml
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
<PlatformTriplet>i686-linux-gnu</PlatformTriplet>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<PlatformTriplet>x86_64-linux-gnu</PlatformTriplet>
</PropertyGroup>
```

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

用于自定义库目录以及头文件位置。此路径会通过`--sysroot`传递给编译器以及链接器。示例:
```xml
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<Sysroot>urs/opt/mysysroot</Sysroot>
</PropertyGroup>
```

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

Expand Down
69 changes: 20 additions & 49 deletions Samples/ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,20 @@
# MSBuildCppCrossToolset 示例项目

## 使用方法

### 配置工具链

## 示例列表
首先,我们从 [GitHub Release](https://github.com/Chuyu-Team/MSBuildCppCrossToolset/releases) 下载 MSBuildCppCrossToolset 并解压。

假设最终解压目录是 `/home/mouri/Workspace/MSBuildCppCrossToolsetRelease/`。我们执行下面这条命令:

```
export VCTargetsPath=/home/mouri/Workspace/MSBuildCppCrossToolsetRelease/VCTargets/v170/
```

### 编译项目
一般来说,Platform拥有以下几种可能:

| 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 | 不支持
假设最终解压目录是:`/home/mouri/Workspace/MSBuildCppCrossToolset/`

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


交叉编译时,必须安装对应的Triplet,比如对于一个x64的Ubuntu,使用GCC编译ARM64小端的Linux程序,则先安装`g++-aarch64-linux-gun`
### [HelloWorld----C++](HelloWorld----C++/HelloWorldApplication.vcxproj)
一个简单的向控制台输出一段文本的应用,支持Windows、Linux以及MacOS。
* 假设项目位置为: `/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples`
* MSBuildCppCrossToolset二进制产物解压到: `/home/mouri/Workspace/MSBuildCppCrossToolset/`

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

; 假设项目位置为 `/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples`。并且编译一些 Release,那么可以输入如下命令:
; 设置 MSBuildCppCrossToolset 目录
export VCTargetsPath=/home/mouri/Workspace/MSBuildCppCrossToolset/VCTargets/v170/
```
; Linux、MacOS下编译 x86
; Linux下编译 x86
dotnet msbuild '/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples/HelloWorld----C++/HelloWorldApplication.vcxproj' '-p:Configuration=Release;Platform=x86'
; Linux、MacOS下编译 x64
Expand All @@ -48,26 +25,20 @@ dotnet msbuild '/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples/He
```

我们也可以在Windows平台编译`HelloWorldApplication.vcxproj`,命令如下:
```
; Windows下编译 x86,注意它叫 Win32!
msbuild "D:\MSBuildCppCrossToolsetWorkspace\Samples\HelloWorld----C++\HelloWorldApplication.vcxproj" -p:Configuration=Release;Platform=Win32
; Windows下编译 x64
msbuild "D:\MSBuildCppCrossToolsetWorkspace\Samples\HelloWorld----C++\HelloWorldApplication.vcxproj" -p:Configuration=Release;Platform=x64

; Windows下编译 ARM64
msbuild "D:\MSBuildCppCrossToolsetWorkspace\Samples\HelloWorld----C++\HelloWorldApplication.vcxproj" -p:Configuration=Release;Platform=ARM64
### [HelloWorld----ObjectC++](HelloWorld----ObjectC++/HelloWorldApplication.vcxproj)
使用`ObjectC++`输出一段文本的应用,注意这个示例仅支持MacOS。
* 假设项目位置为: `/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples/`
* MSBuildCppCrossToolset二进制产物解压到: `/home/mouri/Workspace/MSBuildCppCrossToolset/`

```
; 设置 MSBuildCppCrossToolset 目录
export VCTargetsPath=/home/mouri/Workspace/MSBuildCppCrossToolset/VCTargets/v170/
## 示例列表

### [HelloWorld----C++](HelloWorld----C++/HelloWorldApplication.vcxproj)

一个简单的向控制台输出一段文本的应用,支持Windows、Linux以及MacOS。
; MacOS下编译 x64
dotnet msbuild '/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples/HelloWorld----ObjectC++/HelloWorldApplication.vcxproj' '-p:Configuration=Release;Platform=x64'
; MacOS下编译 ARM64
dotnet msbuild '/home/mouri/Workspace/MSBuildCppCrossToolsetWorkspace/Samples/HelloWorld----ObjectC++/HelloWorldApplication.vcxproj' '-p:Configuration=Release;Platform=ARM64'
### [HelloWorld----ObjectC++](HelloWorld----ObjectC++/HelloWorldApplication.vcxproj)

使用`ObjectC++`输出一段文本的应用,注意这个示例仅支持MacOS。
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<PlatformArchitecture>ARM64</PlatformArchitecture>
<PlatformTarget>ARM64</PlatformTarget>
<PlatformNativeArch>armv7</PlatformNativeArch>
<PlatformTriplet>aarch64-linux-gnu</PlatformTriplet>
<PlatformTriplet>aarch64-apple-darwin</PlatformTriplet>
</PropertyGroup>

<!-- Import After -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<PlatformTarget>x64</PlatformTarget>
<PlatformNativeArch>x86_64</PlatformNativeArch>
<ThumbMode Condition="'$(ThumbMode)' == ''">Disabled</ThumbMode>
<PlatformTriplet>x86_64-linux-gnu</PlatformTriplet>
<PlatformTriplet>x86_64-apple-darwin</PlatformTriplet>
</PropertyGroup>

<!-- Import After -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<PlatformTarget>x86</PlatformTarget>
<PlatformNativeArch>i386</PlatformNativeArch>
<ThumbMode Condition="'$(ThumbMode)' == ''">Disabled</ThumbMode>
<PlatformTriplet>i686-linux-gnu</PlatformTriplet>
<PlatformTriplet>i686-apple-darwin</PlatformTriplet>
</PropertyGroup>

<!-- Import After -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<Import Project="$(VCTargetsPath)\Application Type\$(ApplicationType)\$(ApplicationTypeRevision)\YY.OSX.Cross.props" />

<PropertyGroup>
<Sysroot></Sysroot>
<ClangTarget></ClangTarget>
<ClangTarget>$(PlatformTriplet)</ClangTarget>
<ToolExe>clang++</ToolExe>
<RemoteCCompileToolExe Condition="'$(RemoteCCompileToolExe)' == ''">clang++</RemoteCCompileToolExe>
<RemoteCppCompileToolExe Condition="'$(RemoteCppCompileToolExe)' == ''">clang++</RemoteCppCompileToolExe>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildThisFileDirectory)ImportBefore\*.props" Condition="Exists('$(MSBuildThisFileDirectory)ImportBefore')" />

<Import Project="$(VCTargetsPath)\Application Type\$(ApplicationType)\$(ApplicationTypeRevision)\YY.Linux.Cross.props" />
<Import Project="$(VCTargetsPath)\Application Type\$(ApplicationType)\$(ApplicationTypeRevision)\YY.OSX.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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
</PropertyGroup>

<!-- Linux specific targets to override ClCompile, Link & Lib -->
<Import Project="$(LinuxCommonTargetsPath)\YY.Linux.Cross.targets" />
<Import Project="$(LinuxCommonTargetsPath)\YY.OSX.Cross.targets" />

<Import Project="$(MSBuildThisFileDirectory)ImportAfter\*.targets" Condition="Exists('$(MSBuildThisFileDirectory)ImportAfter')" />
</Project>
6 changes: 5 additions & 1 deletion YY.Build.Cross.Tasks/Targets/OSX/YY.OSX.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 @@ -166,6 +168,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
ObjCAutomaticRefCountingExceptionHandlingSafe="%(ClCompile.ObjCAutomaticRefCountingExceptionHandlingSafe)"
ObjCExceptionHandling="%(ClCompile.ObjCExceptionHandling)"
Sysroot="%(ClCompile.Sysroot)"
TargetArch="%(ClCompile.TargetArch)"
TrackerLogDirectory="$(TLogLocation)"
TLogReadFiles="$(TLogLocation)\compile.read.1.tlog"
TLogWriteFiles="$(TLogLocation)\compile.write.1.tlog"
Expand Down Expand Up @@ -212,7 +215,8 @@ Copyright (C) Microsoft Corporation. All rights reserved.
BuildingInIde="%(Link.BuildingInIde)"
EnableASAN="%(Link.EnableASAN)"
UseOfStl="%(Link.UseOfStl)"
Sysroot="%(ClCompile.Sysroot)"
Sysroot="$(Sysroot)"
TargetArch="$(ClangTarget)"
Frameworks="%(Link.Frameworks)"
TrackerLogDirectory="$(TLogLocation)"
TLogReadFiles="$(TLogLocation)\link.read.1.tlog"
Expand Down

0 comments on commit 195e17b

Please sign in to comment.