CoreCLR登陆GitHub之后,体验CoreCLR首当其冲的方式就是在自己的电脑上编译它,昨天分别在Windows与Linux上成功编译了CoreCLR,详见:

1)Windows上成功编译CoreCLR源代码 ;

2)Linux上成功编译CoreCLR源代码

Windows与Linux上编译成功之后,有一个挡不住的冲动——在Mac上编译CoreCLR。虽然微软目前优先考虑的是Windows与Linux两个平台,CoreCLR的编译暂时不支持Mac OS X,但我最期待的却是在Mac OS X上编译CoreCLR,而且编译CoreCLR所需要的CMake与LLVM在Mac OS X上都有,尝试一下是必须的。

于是,心动不如行动,开始了Mac OS X编译CoreCLR之旅。

Build操作步骤如下:

1)签出github上的CoreCLR代码库: git clone https://github.com/dotnet/coreclr.git

2)安装cmake: brew install cmake

3)运行build命令: sh build.sh

3)build结果-失败,错误信息如下:

Unable to locate llvm-ar
Failed to generate native component build project!

错误信息显示找不到llvm-ar命令。

运行命令 clang --version ,确认LLVM 3.5已安装:

Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)

运行命令 ls /usr/bin/llvm* ,的确没有llvm-ar:

/usr/bin/llvm-g++	/usr/bin/llvm-gcc

后来发现原来藏在 /usr/local/opt/llvm/bin/ 文件夹中:

ls /usr/local/opt/llvm/bin/llvm-ar
/usr/local/opt/llvm/bin/llvm-ar

但环境变量$PATH中没有这个路径,于是加上这个路径:

export PATH=/usr/local/opt/llvm/bin:$PATH

(补充:持久添加到$PATH,sudo vi /etc/paths.d/llvm,添加内容/usr/local/opt/llvm/bin,保存并重新打开Terminal)

添加之后,继续build,“Unable to locate llvm-ar”错误消失。

但出现了新的错误:

-- The C compiler identification is AppleClang 6.0.0.6000056
-- The CXX compiler identification is AppleClang 6.0.0.6000056
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++
-- Check for working CXX compiler: /usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:317 (message):
Not Implemented!

查看CMakeLists.txt中的代码:

if (IS_64BIT_BUILD EQUAL )
if (CLR_CMAKE_PLATFORM_UNIX_TARGET_AMD64)
add_definitions(-DDBG_TARGET_AMD64_UNIX)
endif (CLR_CMAKE_PLATFORM_UNIX_TARGET_AMD64)
add_definitions(-D_TARGET_AMD64_=)
add_definitions(-DDBG_TARGET_AMD64)
else (IS_64BIT_BUILD EQUAL )
# TODO: Support this
message(FATAL_ERROR "Not Implemented!") #line 317
endif (IS_64BIT_BUILD EQUAL )

第317代码是 message(FATAL_ERROR "Not Implemented!") 。分析这段if代码块,可以知道当操作系统是Mac OS X时,IS_64BIT_BUILD的值不为1,如果将之设置为1就可以避开这个错误。

于是顺藤摸瓜,在CMakeLists.txt的第128行找到了设置IS_64BIT_BUILD的代码:

elseif (CLR_CMAKE_PLATFORM_UNIX)
# Set flag to indicate if this will be a 64bit Linux build
if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
set(IS_64BIT_BUILD 1)
endif (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)

当CLR_CMAKE_PLATFORM_UNIX为true的时候,会将IS_64BIT_BUILD的值设置为1。

继续顺藤摸瓜,在CMakeLists.txt的第7行找到了设置CLR_CMAKE_PLATFORM_UNIX的代码:

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(CLR_CMAKE_PLATFORM_UNIX 1)
set(CLR_CMAKE_PLATFORM_UNIX_TARGET_AMD64 1)
endif(CMAKE_SYSTEM_NAME STREQUAL Linux)

然后依葫芦画瓢,添加了针对Mac OS X的代码:

# Mac OS X
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(CLR_CMAKE_PLATFORM_UNIX 1)
set(CLR_CMAKE_PLATFORM_UNIX_TARGET_AMD64 1)
endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)

然后运行./build.sh编译CoreCLR,终于将build.sh跑起来了。虽然也有一些报错,但是build的执行没有中断:

Commencing CoreCLR Repo build
Checking pre-requisites...
Commencing build of native components for amd64/debug
Invoking cmake with arguments: /git/dotnet/coreclr DEBUG
Detected Linux x86_64
-- Configuring done
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
--help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning. MACOSX_RPATH is not specified for the following targets: coreclr
mscordaccore This warning is for project developers. Use -Wno-dev to suppress it. -- Generating done
-- Build files have been written to: /git/dotnet/coreclr/binaries/CMake
Executing make
./build.sh: line 84: nproc: command not found
[ 0%] Building C object src/pal/tools/cppmunge/CMakeFiles/cppmunge.dir/cppmunge.c.o
[ 1%] [ 1%] Built target palrt
[ 1%] Built target mdhotdata_full
Built target gcinfo
/git/dotnet/coreclr/src/pal/tools/cppmunge/cppmunge.c:38:10: fatal error: 'linux/limits.h' file not found
#include <linux/limits.h>
^
[ 1%] [ 1%] Built target ildbsymlib
Built target dbgutil
[ 2%] Built target corguids
[ 3%] Built target ceefgen

满怀期望地等待着build的结果。。。

但是在build过程中,MacBook的CPU风扇突然呼呼作响,接着OS X系统停止响应,只能强制关机。

开机后再尝试,在build过程中依然会让Mac挂掉。接着进行多次尝试,只要build,Mac必挂。

Mac OS X上编译CoreCLR之旅因为这个暂时无法解决的问题而中断。

虽然这次尝试失败了,但是在Mac OS X上编译CoreCLR的痴心不改,在Mac OS X上开发.NET程序的期待不变!

【更新】

“CPU风扇突然呼呼作响,OS X停止响应”可能与“nproc: command not found”这个错误有关,很可能是只用了一个CPU核在编译。

果然是这个原因,将build.sh中的 make install -j `nproc` $__UnprocessedBuildArgs 改为下面的代码,“CPU风扇突然呼呼作响,OS X停止响应”的问题解决:

if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux
make install -j `nproc` $__UnprocessedBuildArgs
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
make install -j `sysctl -n hw.ncpu` $__UnprocessedBuildArgs
fi

后来,微软的@Xy Ziemba提供了更好的解决方法:getconf _NPROCESSORS_ONLN(既支持Linux,又支持Mac OS X), 并且已经将修改提交至coreclr的代码库:

后来,在Issue#102的回复中得知@Geoff Norton已经修改出了一个可在Mac OS X上编译的版(详见#105),于是git签出这个试了一下:

git clone -b osx --single-branch https://github.com/kangaroo/coreclr.git
./build.sh

结果成功了!

[100%] Built target corerun
Install the project...
-- Install configuration: "DEBUG"
-- Installing: /git/dotnet/coreclr-osx/coreclr/binaries/Product/amd64/debug/./corerun
-- Installing: /git/dotnet/coreclr-osx/coreclr/binaries/Product/amd64/debug/./libmscordaccore.dylib
-- Installing: /git/dotnet/coreclr-osx/coreclr/binaries/Product/amd64/debug/./libcoreclr.dylib
Repo successfully built.
Product binaries are available at /git/dotnet/coreclr-osx/coreclr/binaries/Product/amd64/debug

耶!

2月7日下午发现,惊喜地发现可在Mac OS X上编译的版本已经合并到主分支,详见 Merge pull request #117 from kangaroo/osx

编译这个版本时出现"ld: symbol(s) not found for architecture x86_64"错误,删除binaries文件夹之后重新编译,问题解决。

【相关链接】

mac os x build error: CMake Error at CMakeLists.txt: Not Implemented #102

Use # of processors + 1 available to OS scheduler on Linux when building #100

MacOSX Support #105

Initial Mac OSX Support #117

Mac OS X上尝试编译CoreCLR源代码的更多相关文章

  1. Windows上成功编译CoreCLR源代码

    昨天得知微软在GitHub上发布CoreCLR的源代码之后,立马从GitHub上签出代码,并尝试在Windows Server 2012上进行编译. 参考CoreCLR的开发者指南(Developer ...

  2. Linux上成功编译CoreCLR源代码

    >>Build日期:2015-2-5下午(编译失败). 开始Linux发行版用的是CentOS 6.5,操作步骤: 1)配置git: git config --global http.ss ...

  3. 在Mac OS X上用自己编译出的CoreCLR运行.NET程序

    当昨天被Mac OS X上无法编译CoreCLR的问题困扰时(详见Mac OS X上尝试编译CoreCLR源代码),后来发现这个难题竟然被神人@kangaroo给解决了,连CoreCLR的微软开发人员 ...

  4. Mac OS X上用CoreCLR运行一个真正的.NET控制台程序

    这个真正的控制台程序来自corefxlab,名叫CoreClrHelloWorld,是一个跨平台的.NET控制台演示程序,可以显示微软.Linux.苹果的logo. CoreClrHelloWorld ...

  5. Mac OS X 上安装 ASP.NET 5

    在Mac OS X Yosemite 10.10.3 中搭建第一个 ASP.NET 5 Web 项目 终于有时间在 Mac 上安装一下 ASP.NET 5,网上有许多教程,但是多数的时间比较早了,版本 ...

  6. 如何在Mac OS X上安装 Ruby运行环境

    对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境.此安装方法同样适用于产品环境! 系统需求 首先确定操 ...

  7. 在 Mac OS X 上创建的 .NET 命令行程序访问数据库 (使用Entity Framework 7 )

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  8. phpMyAdmin在Mac OS X上的配置和使用

    本文主要记录phpMyAdmin在Mac OS X上的配置和使用,避免朋友们走弯路,浪费不必要的时间.   1. 下载:    2. 在"设置"中打开" web shar ...

  9. Mac OS X上安装 Ruby运行环境

    环境   对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境.此安装方法同样适用于产品环境! 系统需求 ...

随机推荐

  1. guava cache

    适用场景 缓存在很多场景下都是相当有用的.例如,计算或检索一个值的代价很高,并且对同样的输入需要不止一次获取值的时候,就应当考虑使用缓存. Guava Cache与ConcurrentMap很相似,但 ...

  2. uniDBGrid导入数据库(转红鱼儿)

    有朋友问如何将excel导入数据库,这是我做的uniGUI项目中代码,实现uniDBGrid导入数据库的函数,因为用了kbmMW,所以你看到是将uniDBGrid导入kbmMWClientQuery, ...

  3. highcharts 统计的样式

    highcharts 官网:http://www.hcharts.cn/

  4. mysql的sql_mode 模式修改 my.cnf

    1. sql_mode模式 mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode: mysql ...

  5. nullcon HackIM2016 -- Programming Question 3

    Still Hungry and unsutisfied, you are looking for more. Some more, unique un heard dishes. Then you ...

  6. JavaScript初学者应注意的七个细节(转)

    http://www.cnblogs.com/lhb25/archive/2011/01/10/1932284.html 每种语言都有它特别的地方,对于JavaScript来说,使用var就可以声明任 ...

  7. P2001xor-sigma 字典树,然而好坑

    https://vijos.org/p/2001 设perXor[i]表示1---i的前缀异或值. 那么要得到某一段的异或值,只需要perXor[j] ^ perXor[i - 1] 那么我们把per ...

  8. RHEL 7.0 本地配置yum源

    RHEL 7.0 本地配置yum源  yum简介  yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它 ...

  9. extjs,清空treepanel数据。

    extjs,清空treepanel数据. //调用 var rootNode = tree.getRootNode(); removeChildrenData(rootNode); //清理节点的数据 ...

  10. java多线程学习-同步之线程通信

    这个示例是网上烂大街的,子线程循环100次,主线程循环50次,但是我试了很多次,而且从网上找了很多示例,其实多运行几次,看输出结果并不正确.不知道是我转牛角尖了,还是怎么了.也没有大神问,好痛苦.现在 ...