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

添加之后,继续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程序的期待不变!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。