Mac OS X上尝试编译CoreCLR源代码

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 1)
  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_=1)
  add_definitions(-DDBG_TARGET_AMD64)
else (IS_64BIT_BUILD EQUAL 1)
  # TODO: Support this
  message(FATAL_ERROR "Not Implemented!") #line 317
endif (IS_64BIT_BUILD EQUAL 1)

第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

posted @ 2015-02-06 13:25  dudu  阅读(6782)  评论(21编辑  收藏  举报