Add wamrc AoT compiler building support for Windows(MSVC) (#332)

This commit is contained in:
sophy228
2020-08-11 11:30:51 +08:00
committed by GitHub
parent 3be29c3f46
commit 8ad9c1775f
23 changed files with 1186 additions and 23 deletions

View File

@ -1,8 +1,14 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 2.8)
project (aot-compiler)
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
project (aot-compiler)
else()
project (aot-compiler C ASM)
enable_language (ASM_MASM)
endif()
if (NOT DEFINED WAMR_BUILD_PLATFORM)
set (WAMR_BUILD_PLATFORM "linux")
@ -29,6 +35,11 @@ if (NOT WAMR_BUILD_TARGET)
# Build as X86_32 by default in 32-bit platform
set (WAMR_BUILD_TARGET "X86_32")
endif ()
if (WAMR_BUILD_PLATFORM STREQUAL "windows")
if (("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "Win32"))
set (WAMR_BUILD_TARGET "X86_32")
endif()
endif()
endif ()
string(TOUPPER ${WAMR_BUILD_TARGET} WAMR_BUILD_TARGET)
@ -71,10 +82,18 @@ message ("-- CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})
# Enable LLVM
set (LLVM_SRC_ROOT "${PROJECT_SOURCE_DIR}/../core/deps/llvm")
if (NOT EXISTS "${LLVM_SRC_ROOT}/build")
message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build")
if (WAMR_BUILD_PLATFORM STREQUAL "windows")
if (NOT EXISTS "${LLVM_SRC_ROOT}/win32build")
message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/win32build")
endif ()
set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/win32build;${CMAKE_PREFIX_PATH}")
else()
if (NOT EXISTS "${LLVM_SRC_ROOT}/build")
message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build")
endif ()
set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}")
endif ()
set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}")
find_package(LLVM REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
@ -83,10 +102,17 @@ message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
if(NOT MSVC)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
else()
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
endif()
endif()
if (NOT MSVC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic")
endif()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic")
set (SHARED_DIR ../core/shared)
set (IWASM_DIR ../core/iwasm)
@ -108,20 +134,28 @@ include (${IWASM_DIR}/interpreter/iwasm_interp.cmake)
include (${IWASM_DIR}/aot/iwasm_aot.cmake)
include (${IWASM_DIR}/compilation/iwasm_compl.cmake)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
if (NOT MSVC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
endif()
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang" OR MSVC))
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
endif()
endif ()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
if (NOT MSVC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
endif()
# We disable these flags by default to stay the same with wasm runtime
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch=thunk -mfunction-return=thunk")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE -ftrapv -D_FORTIFY_SOURCE=2")
if (NOT MSVC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE -ftrapv -D_FORTIFY_SOURCE=2")
endif()
# message ("-- CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
@ -140,5 +174,8 @@ add_library (aotclib ${IWASM_COMPL_SOURCE})
add_executable (wamrc main.c)
target_link_libraries (wamrc aotclib vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)
if (NOT MSVC)
target_link_libraries (wamrc aotclib vmlib LLVMDemangle ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)
else()
target_link_libraries (wamrc aotclib vmlib ${LLVM_AVAILABLE_LIBS})
endif()

View File

@ -0,0 +1,98 @@
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
def clone_llvm():
llvm_dir = Path("llvm")
if(llvm_dir.exists() == False):
print("Clone llvm to core/deps/ ..")
for line in os.popen("git clone --branch release/10.x https://github.com/llvm/llvm-project.git llvm"):
print(line)
else:
print("llvm source codes already existed")
return llvm_dir
""" def detect_VS_version():
program_dirs = [os.environ['ProgramFiles(x86)'], os.environ['ProgramFiles']]
for dir in program_dirs:
vswhere = Path("{}\\Microsoft Visual Studio\\Installer\\vswhere.exe".format(dir))
if (vswhere.exists()):
print('"{}" -version 14.0,16.0'.format(vswhere))
for line in os.popen('"{}" -version 14.0,16.0'.format(vswhere)):
keyvalue = line.split(':', maxsplit=1)
if(keyvalue[0] == "installationPath"):
value = keyvalue[1].strip()
for line in os.popen('"{}\\VC\\Auxiliary\\Build\\vcvars32.bat"'.format(value)):
print(line)
break """
def main():
current_os = sys.platform
print("current OS is ", current_os)
current_dir = Path.cwd()
deps_dir = current_dir.joinpath( "../core/deps")
os.chdir(deps_dir)
llvm_dir = clone_llvm()
os.chdir(llvm_dir)
if(current_os == "linux"):
build_dir_name = "build"
llvm_file = "bin/llvm-lto"
# generator = '"Unix Makefiles"'
elif(current_os == "win32"):
build_dir_name = "win32build"
llvm_file = "LLVM.sln"
# generator = '"Visual Studio 15 2017"'
else:
build_dir_name = "build"
# generator = '""'
Path(build_dir_name).mkdir(exist_ok = True)
build_dir = Path(build_dir_name)
os.chdir(build_dir)
if ( not Path(llvm_file).exists()):
core_number = os.cpu_count()
print("Build llvm with", core_number, " cores")
cmd = 'cmake ../llvm \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DLLVM_TARGETS_TO_BUILD:STRING="X86;ARM;AArch64;Mips" \
-DLLVM_INCLUDE_GO_TESTS=OFF \
-DLLVM_INCLUDE_TOOLS=OFF \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF \
-DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON \
-DLLVM_ENABLE_ZLIB:BOOL=OFF \
-DLLVM_INCLUDE_DOCS:BOOL=OFF \
-DLLVM_INCLUDE_EXAMPLES:BOOL=OFF \
-DLLVM_INCLUDE_TESTS:BOOL=OFF \
-DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF \
-DLLVM_APPEND_VC_REV:BOOL=OFF'
print(cmd)
for line in os.popen(cmd):
print(line)
else:
print("llvm has already been Cmaked")
if(current_os == "linux"):
for line in os.popen("make -j {}".format(core_number)):
print(line)
elif(current_os == "win32"):
print("Please open LLVM.sln in {} to build *Release* version".format(build_dir.absolute()))
os.chdir(current_dir)
if __name__ == "__main__":
main()