d2439ba47a30 — Vesa Norilo 5 months ago
binaryen is now an externalproject
4 files changed, 101 insertions(+), 73 deletions(-)

M CMakeLists.txt
M cmake/Binaryen.cmake
M src/backends/BinaryenEmitter.cpp
M src/backends/BinaryenModule.cpp
M CMakeLists.txt +2 -2
@@ 126,7 126,7 @@ set( KRONOS_CORE_SOURCES
 	"src/kronos_abi.h"
 )
 
-set(KRONOS_BINARYEN ON CACHE BOOL "Attempt to user Binaryen")
+set(KRONOS_BINARYEN ON CACHE BOOL "Attempt to use Binaryen")
 
 if (KRONOS_BINARYEN OR EMSCRIPTEN)
 	message(STATUS "Using Binaryen backend")

          
@@ 420,7 420,7 @@ execute_process(
 )
 
 if(NOT LIBRARY_VERSION_RESULT EQUAL 0)
-	message(FATAL "Failed to determine library version")
+	message(FATAL_ERROR "Failed to determine library version")
 endif()
 
 if(NOT ${HG_VERSION_STRING} VERSION_LESS 4)

          
M cmake/Binaryen.cmake +95 -67
@@ 1,67 1,95 @@ 
-set(CMAKE_FIND_ROOT_PATH "")
-set(CMAKE_POSITION_INDEPENDENT_CODE False)
-
-if (EMSCRIPTEN)
-	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O2 -s STACK_OVERFLOW_CHECK=2 -s WARN_UNALIGNED=1 -s DEMANGLE_SUPPORT=1 -s DISABLE_EXCEPTION_CATCHING=0 -s DISABLE_EXCEPTION_THROWING=0 -s ASSERTIONS=2")
-	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -s DISABLE_EXCEPTION_CATCHING=1 -s USE_CLOSURE_COMPILER=1")
-	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-align -Wover-aligned -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -s MODULARIZE=1 --bind")
-endif()
-
-foreach(dep binaryen passes wasm asmjs emscripten-optimizer ir cfg support)
-	set(varname "lib_${dep}")
-	find_library(${varname} NAMES ${dep} HINTS ${CMAKE_BINARY_DIR}/../binaryen/lib PATHS ${BINARYEN_LIBRARY_DIR} NO_CMAKE_FIND_ROOT_PATH)
-	list(APPEND BINARYEN_LIBRARIES ${${varname}})
-endforeach()
-
-if(NOT ${lib_binaryen} STREQUAL "lib_binaryen-NOTFOUND")
-	message(STATUS "Linking Binaryen")
-	add_library(binaryen_backend
-		"src/backends/BinaryenInterface.cpp"
-		"src/backends/BinaryenEmitter.cpp"
-		"src/backends/BinaryenEmitter.h"
-		"src/backends/BinaryenModule.cpp"
-		"src/backends/BinaryenCompiler.cpp"
-		"src/backends/BinaryenModule.h"
-		"src/backends/BinaryenCompiler.h"
-		"src/backends/GenericModule.h"
-		"src/backends/CodeGenModule.h")
-
-	include_directories("${BINARYEN_DIR}/src")
-	
-	target_link_libraries(binaryen_backend PUBLIC ${BINARYEN_LIBRARIES})
-	set_target_properties( binaryen_backend PROPERTIES FOLDER libs/emitters )
-	target_include_directories( binaryen_backend PRIVATE ${BINARYEN_INCLUDE_DIR} )
-
-	set(HAVE_BINARYEN True)
-endif()
-
-if(EMSCRIPTEN)
-	if (${lib_binaryen} STREQUAL "lib_binaryen-NOTFOUND")
-		message(FATAL_ERROR "Binaryen is required when compiling for wasm")
-	endif()
-
-	file(COPY ${CMAKE_SOURCE_DIR}/library DESTINATION ${CMAKE_BINARY_DIR} FILES_MATCHING PATTERN *.k)
-	file(COPY ${CMAKE_SOURCE_DIR}/library DESTINATION ${CMAKE_BINARY_DIR} FILES_MATCHING PATTERN *.json)
-
-	# veneer runtime
-	add_executable(veneer
-		src/lithe/lithe.cpp
-		src/lithe/ast.cpp
-		src/driver/veneer.cpp
-		src/lithe/grammar/kronos.cpp
-		src/lithe/grammar/common.cpp)
-
-
-	# compiler
-    add_executable(kronos src/driver/kwasm.cpp ${KRONOS_CORE_SOURCES})
-	target_link_libraries(kronos PRIVATE binaryen_backend tinyxml grammar_kronos grammar_json platform)
-	set_property(TARGET veneer APPEND_STRING PROPERTY COMPILE_FLAGS "-s NO_FILESYSTEM=1 -fno-exceptions")
-
-	# this is CMake at its best.
-	target_link_libraries(kronos PUBLIC "--preload-file ${CMAKE_BINARY_DIR}/library@library" "-s TOTAL_STACK=64MB -s TOTAL_MEMORY=128MB -s 'EXPORT_NAME=\"NativeCompiler\"'")
-	target_link_libraries(veneer PUBLIC "-fno-exceptions -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=16MB -s TOTAL_MEMORY=32MB" "-s NO_FILESYSTEM=1" "-s EXPORTED_FUNCTIONS='[\"_vnr_malloc\",\"_vnr_free\",\"_vnr_memset\"]'" "-s 'EXPORT_NAME=\"NativeParser\"'")
-
-	if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
-		target_link_libraries(kronos PUBLIC "-g3")
-	endif()
-endif()
+set(CMAKE_FIND_ROOT_PATH "")
+set(CMAKE_POSITION_INDEPENDENT_CODE False)
+
+set_property(DIRECTORY PROPERTY EP_BASE "${CMAKE_BINARY_DIR}/external")
+set(BINARYEN_BUILD_DIR "${CMAKE_BINARY_DIR}/external/Build/binaryen")
+
+include(ExternalProject)
+
+set(BINARYEN_VERSION version_84)
+
+if (EMSCRIPTEN) 
+	set(CMAKE_CMD "emcmake")
+	set(CMAKE_ARGS "cmake" "-DENABLE_WERROR=OFF" "-DBUILD_STATIC_LIB=ON")
+else()
+	set(CMAKE_CMD ${CMAKE_COMMAND})
+	set(CMAKE_ARGS "-DENABLE_WERROR=OFF" "-DBUILD_STATIC_LIB=ON")
+endif()
+
+if (WIN32)
+	ExternalProject_Add(
+		binaryen 
+		SVN_REPOSITORY "https://github.com/WebAssembly/binaryen.git/tags/${BINARYEN_VERSION}"
+		CMAKE_COMMAND "${CMAKE_CMD}"
+		CMAKE_ARGS ${CMAKE_ARGS}
+		BUILD_COMMAND cmake --build "${BINARYEN_BUILD_DIR}" --target binaryen
+		INSTALL_COMMAND "")
+else()
+	ExternalProject_Add(
+		binaryen 
+		GIT_REPOSITORY https://github.com/WebAssembly/binaryen.git
+		GIT_TAG ${BINARYEN_VERSION}
+		CMAKE_COMMAND "${CMAKE_CMD}"
+		CMAKE_ARGS ${CMAKE_ARGS}
+		BUILD_COMMAND cmake --build "${BINARYEN_BUILD_DIR}" --target binaryen
+		INSTALL_COMMAND "")
+endif()
+
+if (EMSCRIPTEN)
+	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O2 -s STACK_OVERFLOW_CHECK=2 -s WARN_UNALIGNED=1 -s DEMANGLE_SUPPORT=1 -s DISABLE_EXCEPTION_CATCHING=0 -s DISABLE_EXCEPTION_THROWING=0 -s ASSERTIONS=2")
+	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -s DISABLE_EXCEPTION_CATCHING=1 -s USE_CLOSURE_COMPILER=1")
+	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-align -Wover-aligned -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -s MODULARIZE=1 --bind")
+endif()
+
+foreach(dep binaryen passes wasm asmjs emscripten-optimizer ir cfg support)
+	set(PATH "${BINARYEN_BUILD_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${dep}${CMAKE_STATIC_LIBRARY_SUFFIX}")
+	list(APPEND BINARYEN_LIBRARIES "${PATH}")
+endforeach()
+
+message(STATUS "Linking Binaryen ${BINARYEN_LIBRARIES}")
+add_library(binaryen_backend
+	"src/backends/BinaryenInterface.cpp"
+	"src/backends/BinaryenEmitter.cpp"
+	"src/backends/BinaryenEmitter.h"
+	"src/backends/BinaryenModule.cpp"
+	"src/backends/BinaryenCompiler.cpp"
+	"src/backends/BinaryenModule.h"
+	"src/backends/BinaryenCompiler.h"
+	"src/backends/GenericModule.h"
+	"src/backends/CodeGenModule.h")
+
+include_directories("${BINARYEN_DIR}/src")
+	
+target_link_libraries(binaryen_backend PUBLIC ${BINARYEN_LIBRARIES})
+set_target_properties( binaryen_backend PROPERTIES FOLDER libs/emitters )
+target_include_directories( binaryen_backend PRIVATE ${BINARYEN_INCLUDE_DIR} )
+add_dependencies(binaryen_backend binaryen)
+
+set(HAVE_BINARYEN True)
+
+if(EMSCRIPTEN)
+	file(COPY ${CMAKE_SOURCE_DIR}/library DESTINATION ${CMAKE_BINARY_DIR} FILES_MATCHING PATTERN *.k)
+	file(COPY ${CMAKE_SOURCE_DIR}/library DESTINATION ${CMAKE_BINARY_DIR} FILES_MATCHING PATTERN *.json)
+
+	# veneer runtime
+	add_executable(veneer
+		src/lithe/lithe.cpp
+		src/lithe/ast.cpp
+		src/driver/veneer.cpp
+		src/lithe/grammar/kronos.cpp
+		src/lithe/grammar/common.cpp)
+
+
+	# compiler
+    add_executable(kronos src/driver/kwasm.cpp ${KRONOS_CORE_SOURCES})
+	target_link_libraries(kronos PRIVATE binaryen_backend tinyxml grammar_kronos grammar_json platform)
+	set_property(TARGET veneer APPEND_STRING PROPERTY COMPILE_FLAGS "-s NO_FILESYSTEM=1 -fno-exceptions")
+
+	# this is CMake at its best.
+	target_link_libraries(kronos PUBLIC "--preload-file ${CMAKE_BINARY_DIR}/library@library" "-s TOTAL_STACK=64MB -s TOTAL_MEMORY=128MB -s 'EXPORT_NAME=\"NativeCompiler\"'")
+	target_link_libraries(veneer PUBLIC "-fno-exceptions -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=16MB -s TOTAL_MEMORY=32MB" "-s NO_FILESYSTEM=1" "-s EXPORTED_FUNCTIONS='[\"_vnr_malloc\",\"_vnr_free\",\"_vnr_memset\"]'" "-s 'EXPORT_NAME=\"NativeParser\"'")
+
+	if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+		target_link_libraries(kronos PUBLIC "-g3")
+	endif()
+endif()

          
M src/backends/BinaryenEmitter.cpp +2 -2
@@ 537,7 537,7 @@ namespace K3 {
 			auto fty = BinaryenAddFunctionType(M, nullptr, returnTy, paramTys.data(), (int)paramTys.size());
 
 			auto Mod = (wasm::Module*)M;
-			if (Mod->getImportOrNull(sym.c_str()) == nullptr) {
+			if (Mod->getFunctionOrNull(sym.c_str()) == nullptr) {
 				BinaryenAddFunctionImport(M, sym.c_str(), "import", sym.c_str(), fty);
 			}
 

          
@@ 547,7 547,7 @@ namespace K3 {
 			}
 #endif
 
-			auto fncall = BinaryenCallImport(M, sym.c_str(), (BinaryenExpressionRef*)pass.data(), (int)pass.size(), returnTy);
+			auto fncall = BinaryenCall(M, sym.c_str(), (BinaryenExpressionRef*)pass.data(), (int)pass.size(), returnTy);
 
 			if (returnByHeap) {
 				b->instr.emplace_back(fncall);

          
M src/backends/BinaryenModule.cpp +2 -2
@@ 161,7 161,7 @@ namespace K3 {
 				BinaryenAddFunctionExport(M, "UnwindStack", "UnwindStack");
 				BinaryenAddGlobal(M, HEAP_TOP, BinaryenTypeInt32(), 1, BinaryenConst(M, BinaryenLiteralInt32((int)dataSegment.staticDataOffset)));
 			} else {
-				BinaryenAddMemoryImport(M, "0", "import", "heap");
+				BinaryenAddMemoryImport(M, "0", "import", "heap", 0);
 			}
 
 			auto getStackPointer = CreateFunction("GetStackPointer", Int32Ty(), {}, true);

          
@@ 440,7 440,7 @@ namespace K3 {
 				0
 			};
 
-			BinaryenSetMemory(M, (((int)dataBlob.size() + 65536) / 65536), 65535, memExportName, segData, segOffset, segSize, 1);
+			BinaryenSetMemory(M, (((int)dataBlob.size() + 65536) / 65536), 65535, memExportName, segData, segPassive, segOffset, segSize, 1, 0);
 			return dataBlob.size();
 		}