Skip to content

Commit

Permalink
[Tool] Merge tools code to library
Browse files Browse the repository at this point in the history
* Add static tool options
* Add rpath to executables

Signed-off-by: Shen-Ta Hsieh <[email protected]>
  • Loading branch information
ibmibmibm authored and q82419 committed Jul 18, 2022
1 parent 409b1b0 commit dc5a47f
Show file tree
Hide file tree
Showing 14 changed files with 781 additions and 608 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ option(WASMEDGE_BUILD_COVERAGE "Generate coverage report. Require WASMEDGE_BUILD
option(WASMEDGE_BUILD_AOT_RUNTIME "Enable WasmEdge LLVM-based ahead of time compilation runtime." ON)
option(WASMEDGE_BUILD_SHARED_LIB "Generate the WasmEdge shared library." ON)
option(WASMEDGE_BUILD_STATIC_LIB "Generate the WasmEdge static library." OFF)
option(WASMEDGE_BUILD_TOOLS "Generate wasmedge and wasmedgec tools." ON)
option(WASMEDGE_BUILD_TOOLS "Generate wasmedge and wasmedgec tools. Dependence on WasmEdge shared library" ON)
option(WASMEDGE_BUILD_STATIC_TOOLS "Generate static wasmedge and wasmedgec tools. Dependence on WasmEdge static library" OFF)
option(WASMEDGE_BUILD_PLUGINS "Generate plugins." ON)
option(WASMEDGE_BUILD_EXAMPLE "Generate examples." OFF)
option(WASMEDGE_FORCE_DISABLE_LTO "Forcibly disable link time optimization when linking even in Release/RelWithDeb build." OFF)
Expand Down Expand Up @@ -112,7 +113,7 @@ if(WASMEDGE_BUILD_PLUGINS)
add_subdirectory(plugins)
endif()
add_subdirectory(thirdparty)
if(WASMEDGE_BUILD_TOOLS)
if(WASMEDGE_BUILD_TOOLS OR WASMEDGE_BUILD_STATIC_TOOLS)
add_subdirectory(tools)
endif()
if(WASMEDGE_BUILD_EXAMPLE)
Expand Down
13 changes: 12 additions & 1 deletion cmake/Helper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ function(wasmedge_setup_target target)
ENABLE_EXPORTS ON
POSITION_INDEPENDENT_CODE ON
VISIBILITY_INLINES_HIDDEN ON
SKIP_RPATH ON
BUILD_RPATH_USE_ORIGIN ON
MACOSX_RPATH ON
INTERPROCEDURAL_OPTIMIZATION ${WASMEDGE_INTERPROCEDURAL_OPTIMIZATION}
)
target_compile_options(${target}
Expand All @@ -101,4 +102,14 @@ endfunction()
function(wasmedge_add_executable target)
add_executable(${target} ${ARGN})
wasmedge_setup_target(${target})
file(RELATIVE_PATH rel /${CMAKE_INSTALL_BINDIR} /${CMAKE_INSTALL_LIBDIR})
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set_target_properties(${target} PROPERTIES
INSTALL_RPATH "$ORIGIN/${rel}"
)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
set_target_properties(${target} PROPERTIES
INSTALL_RPATH "@executable_path/${rel}"
)
endif()
endfunction()
10 changes: 10 additions & 0 deletions include/api/wasmedge/wasmedge.h
Original file line number Diff line number Diff line change
Expand Up @@ -3139,6 +3139,16 @@ WASMEDGE_CAPI_EXPORT extern void WasmEdge_VMDelete(WasmEdge_VMContext *Cxt);

// <<<<<<<< WasmEdge VM functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>> WasmEdge Driver functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/// Entrypoint for the compiler.
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Compiler(int Argc, const char *Argv[]);

/// Entrypoint for the general tool.
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Tool(int Argc, const char *Argv[]);

// <<<<<<<< WasmEdge Driver functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>> WasmEdge Plugin functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/// Load plugins with default search path.
Expand Down
22 changes: 22 additions & 0 deletions include/driver/compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2019-2022 Second State INC

//===-- wasmedge/driver/compiler.h - Compiler entrypoint ------------------===//
//
// Part of the WasmEdge Project.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contents the entrypoint for the compiler executable.
///
//===----------------------------------------------------------------------===//
#pragma once

namespace WasmEdge {
namespace Driver {

int Compiler(int Argc, const char *Argv[]) noexcept;

} // namespace Driver
} // namespace WasmEdge
22 changes: 22 additions & 0 deletions include/driver/tool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2019-2022 Second State INC

//===-- wasmedge/driver/tool.h - Tool entrypoint --------------------------===//
//
// Part of the WasmEdge Project.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contents the entrypoint for the tooling executable.
///
//===----------------------------------------------------------------------===//
#pragma once

namespace WasmEdge {
namespace Driver {

int Tool(int Argc, const char *Argv[]) noexcept;

} // namespace Driver
} // namespace WasmEdge
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_subdirectory(validator)
add_subdirectory(executor)
add_subdirectory(host)
add_subdirectory(vm)
add_subdirectory(driver)
if(WASMEDGE_BUILD_SHARED_LIB OR WASMEDGE_BUILD_STATIC_LIB)
add_subdirectory(api)
endif()
29 changes: 18 additions & 11 deletions lib/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ wasmedge_add_library(wasmedgeCAPI OBJECT
target_link_libraries(wasmedgeCAPI
PUBLIC
wasmedgeVM
wasmedgeDriver
)

if (WASMEDGE_BUILD_AOT_RUNTIME)
Expand Down Expand Up @@ -153,6 +154,7 @@ if(WASMEDGE_BUILD_STATIC_LIB)
endif()

if(NOT APPLE)
find_package(ZLIB REQUIRED)
# Pack the zlib.
get_filename_component(ZLIB_PATH "${ZLIB_LIBRARIES}" DIRECTORY)
wasmedge_add_libs_component_command("z" ${ZLIB_PATH})
Expand All @@ -165,21 +167,26 @@ if(WASMEDGE_BUILD_STATIC_LIB)
wasmedge_add_static_lib_component_command(wasmedgeAOT)
endif()

add_custom_target(wasmedge_c_static ALL
${WASMEDGE_STATIC_LIB_CMDS}
COMMAND ${CMAKE_AR} -qcs libwasmedge_c.a $<TARGET_OBJECTS:wasmedgeCAPI> objs/*/*.o
COMMAND ${CMAKE_COMMAND} -E rm -rf objs
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${WASMEDGE_STATIC_LIB_DEPS}
wasmedge_add_library(wasmedge_c_static STATIC
../../include/api/wasmedge/wasmedge.h
)

target_link_libraries(wasmedge_c_static
PRIVATE
wasmedgeCAPI
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libwasmedge_c.a
DESTINATION ${CMAKE_INSTALL_LIBDIR}
target_include_directories(wasmedge_c_static
PUBLIC
${PROJECT_BINARY_DIR}/include/api
${PROJECT_SOURCE_DIR}/include/api
)
install(FILES ${WASMEDGE_CAPI_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/wasmedge

set_target_properties(wasmedge_c_static PROPERTIES OUTPUT_NAME wasmedge_c)

install(TARGETS wasmedge_c_static
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/wasmedge
)

endif()
17 changes: 16 additions & 1 deletion lib/api/wasmedge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "wasmedge/wasmedge.h"

#include "aot/compiler.h"
#include "driver/compiler.h"
#include "driver/tool.h"
#include "host/wasi/wasimodule.h"
#include "plugin/plugin.h"
#include "vm/vm.h"
Expand Down Expand Up @@ -2551,6 +2553,19 @@ WASMEDGE_CAPI_EXPORT void WasmEdge_VMDelete(WasmEdge_VMContext *Cxt) {

// <<<<<<<< WasmEdge VM functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>> WasmEdge Driver functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Compiler(int Argc,
const char *Argv[]) {
return WasmEdge::Driver::Compiler(Argc, Argv);
}

WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Tool(int Argc, const char *Argv[]) {
return WasmEdge::Driver::Tool(Argc, Argv);
}

// <<<<<<<< WasmEdge Driver functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>> WasmEdge Plugin functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

WASMEDGE_CAPI_EXPORT void WasmEdge_Plugin_loadWithDefaultPluginPaths(void) {
Expand All @@ -2559,7 +2574,7 @@ WASMEDGE_CAPI_EXPORT void WasmEdge_Plugin_loadWithDefaultPluginPaths(void) {
}
}

// <<<<<<<< WasmEdge Pluginfunctions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// <<<<<<<< WasmEdge Plugin functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#ifdef __cplusplus
} // extern "C"
Expand Down
33 changes: 33 additions & 0 deletions lib/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2019-2022 Second State INC

wasmedge_add_library(wasmedgeDriver
compiler.cpp
tool.cpp
)

if(WASMEDGE_BUILD_AOT_RUNTIME)
target_link_libraries(wasmedgeDriver
PRIVATE
wasmedgeLoader
wasmedgeCommon
wasmedgePO
wasmedgeVM
wasmedgeAOT
)
else()
target_link_libraries(wasmedgeDriver
PRIVATE
wasmedgeLoader
wasmedgeCommon
wasmedgePO
wasmedgeVM
)
endif()

if(WASMEDGE_BUILD_AOT_RUNTIME)
target_compile_definitions(wasmedgeDriver
PRIVATE
-DWASMEDGE_BUILD_AOT_RUNTIME
)
endif()
Loading

0 comments on commit dc5a47f

Please sign in to comment.