### AsmTK Configuration Variable Setup Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Sets default paths for AsmTK and AsmJIT, and configures build-related boolean variables like ASMJIT_EXTERNAL, ASMTK_EMBED, and ASMTK_STATIC. These variables are exposed as CACHE variables for user configuration. ```cmake if (NOT DEFINED ASMTK_DIR) set(ASMTK_DIR "${CMAKE_CURRENT_LIST_DIR}") endif() if (NOT DEFINED ASMJIT_DIR) set(ASMJIT_DIR "${ASMTK_DIR}/../asmjit") endif() if (NOT DEFINED ASMJIT_EXTERNAL) set(ASMJIT_EXTERNAL FALSE) endif() if (NOT DEFINED ASMTK_EMBED) set(ASMTK_EMBED FALSE) endif() if (NOT DEFINED ASMTK_STATIC) set(ASMTK_STATIC ${ASMJIT_EMBED}) endif() set(ASMTK_DIR "${ASMTK_DIR}" CACHE PATH "Location of 'asmtk'") set(ASMJIT_DIR "${ASMJIT_DIR}" CACHE PATH "Location of 'asmjit'") set(ASMJIT_EXTERNAL ${ASMJIT_EXTERNAL} CACHE BOOL "External 'asmjit'") set(ASMTK_TEST ${ASMTK_TEST} CACHE BOOL "Build 'asmtk' test applications") set(ASMTK_EMBED ${ASMTK_EMBED} CACHE BOOL "Embed 'asmtk' library (no targets)") set(ASMTK_STATIC ${ASMTK_STATIC} CACHE BOOL "Build 'asmtk' library as static") ``` -------------------------------- ### CMake Project Initialization and Policy Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Sets the minimum CMake version required and pushes/sets CMake policies for consistent behavior, specifically honoring visibility properties. ```cmake cmake_minimum_required(VERSION 3.8) cmake_policy(PUSH) cmake_policy(SET CMP0063 NEW) # Honor visibility properties. ``` -------------------------------- ### Parse Assembly with AsmParser and Dump Code (C++) Source: https://github.com/asmjit/asmtk/blob/master/README.md Demonstrates using AsmParser to parse assembly instructions and emit them to a BaseEmitter, with a helper function to dump the generated binary code in hexadecimal format. It utilizes AsmJit's environment, CodeHolder, and x86::Assembler. ```c++ #include using namespace asmjit; using namespace asmtk; // Used to print binary code as hex. static void dumpCode(const uint8_t* buf, size_t size) { constexpr uint32_t kCharsPerLine = 39; char hex[kCharsPerLine * 2 + 1]; size_t i = 0; while (i < size) { size_t j = 0; size_t end = size - i < kCharsPerLine ? size - i : size_t(kCharsPerLine); end += i; while (i < end) { uint8_t b0 = buf[i] >> 4; uint8_t b1 = buf[i] & 15; hex[j++] = b0 < 10 ? '0' + b0 : 'A' + b0 - 10; hex[j++] = b1 < 10 ? '0' + b1 : 'A' + b1 - 10; i++; } hex[j] = '\0'; puts(hex); } } int main(int argc, char* argv[]) { // Setup CodeHolder for X64. Environment env(Arch::kX64); CodeHolder code; code.init(env); // Attach x86::Assembler to `code`. x86::Assembler a(&code); // Create AsmParser that will emit to x86::Assembler. AsmParser p(&a); // Parse some assembly. Error err = p.parse( "mov rax, rbx\n" "vaddpd zmm0, zmm1, [rax + 128]\n"); // Error handling (use asmjit::ErrorHandler for more robust error handling). if (err != Error::kOk) { printf("ERROR: %08x (%s)\n", err, DebugUtils::errorAsString(err)); return 1; } // Now you can print the code, which is stored in the first section (.text). CodeBuffer& buffer = code.section_by_id(0)->buffer(); dumpCode(buffer.data(), buffer.size()); return 0; } ``` -------------------------------- ### Define AsmTK Source Files and Add to Target Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt This CMake function, `asmtk_add_source`, collects source files from a specified base directory and adds them to a list that will be used to build the AsmTK library. It also groups the source files for better organization within the build system. ```cmake function(asmtk_add_source out base_dir) set(src_array) foreach(file ${ARGN}) set(src_file "${ASMTK_DIR}/${base_dir}/${file}") list(APPEND src_array ${src_file}) endforeach() source_group(TREE "${ASMTK_DIR}" FILES ${src_array}) set(out_tmp ${${out}}) list(APPEND out_tmp ${src_array}) set("${out}" "${out_tmp}" PARENT_SCOPE) endfunction() set(ASMTK_SRC "") set(ASMTK_SRC_LIST asmtk/asmtk.h asmtk/asmparser.cpp asmtk/asmparser.h asmtk/asmtokenizer.cpp asmtk/asmtokenizer.h asmtk/elfdefs.h asmtk/globals.h asmtk/parserutils.h asmtk/strtod.h ) asmtk_add_source(ASMTK_SRC src ${ASMTK_SRC_LIST}) ``` -------------------------------- ### Configure AsmTK Library Target Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt This section defines the 'asmtk' library target using `add_library`. It sets the C++ standard to C++17, applies various compile options based on configuration (Debug, Release), and links against the AsmJit library. It also configures include directories and sets C++ visibility. ```cmake if (NOT ASMTK_EMBED) add_library(asmtk ${ASMTK_TARGET_TYPE} ${ASMTK_SRC}) target_compile_features(asmtk PUBLIC cxx_std_17) target_compile_options(asmtk PUBLIC ${ASMTK_CFLAGS}) target_compile_options(asmtk PRIVATE ${ASMTK_PRIVATE_CFLAGS} $<$:${ASMTK_PRIVATE_CFLAGS_DBG}> $<$>:${ASMTK_PRIVATE_CFLAGS_REL}>) if(ASMJIT_EXTERNAL) target_link_libraries(asmtk PUBLIC ${ASMJIT_LIBRARY}) find_path(ASMJIT_INCLUDE_DIR NAMES asmjit/core.h) target_include_directories(asmtk PRIVATE ${ASMJIT_INCLUDE_DIR}) else() target_link_libraries(asmtk PUBLIC asmjit::asmjit) endif() target_include_directories(asmtk BEFORE INTERFACE $ $) set_property(TARGET asmtk PROPERTY CXX_VISIBILITY_PRESET hidden) # Add asmjit::asmtk target (alias to asmtk). add_library(asmjit::asmtk ALIAS asmtk) # TODO: [CMAKE] Deprecated alias - we use projectname::libraryname convention now. add_library(AsmTK::AsmTK ALIAS asmtk) # Add AsmTK install instructions (library and public headers). if (NOT ASMTK_NO_INSTALL) install(TARGETS asmtk EXPORT asmtk-config RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") install(EXPORT asmtk-config NAMESPACE asmjit:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/asmtk") foreach(_src_file ${ASMTK_SRC_LIST}) if ("${_src_file}" MATCHES "\.h$" AND NOT "${_src_file}" MATCHES "_p\.h$") get_filename_component(_src_dir ${_src_file} PATH) install(FILES "${ASMTK_DIR}/src/${_src_file}" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_src_dir}") endif() endforeach() endif() if (ASMTK_TEST AND NOT ASMJIT_EMBED) set(ASMTK_SAMPLES_SRC asmtk_test_x86cmd asmtk_test_x86handler asmtk_test_x86parser) foreach(_target ${ASMTK_SAMPLES_SRC}) add_executable(${_target} "${ASMTK_DIR}/test/${_target}.cpp") target_link_libraries(${_target} asmjit::asmjit asmjit::asmtk) target_compile_features(${_target} PUBLIC cxx_std_17) set_property(TARGET ${_target} PROPERTY CXX_VISIBILITY_PRESET hidden) endforeach() endif() endif() cmake_policy(POP) ``` -------------------------------- ### AsmTK Compiler Flags Configuration Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Sets common include directories and initializes C++ compiler flags. It includes conditional logic to append specific flags based on the compiler ID (MSVC, GNU, Clang, AppleClang) for optimization, warnings, and other build-specific settings. ```cmake set(ASMTK_INCLUDE_DIRS "${ASMTK_DIR}/src") # Include directory is the same as source dir. set(ASMTK_CFLAGS "") # Public compiler flags. set(ASMTK_PRIVATE_CFLAGS "") # Private compiler flags independent of build type. set(ASMTK_PRIVATE_CFLAGS_DBG "") # Private compiler flags used by debug builds. set(ASMTK_PRIVATE_CFLAGS_REL "") # Private compiler flags used by release builds. if (NOT ASMTK_NO_CUSTOM_FLAGS) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC") list(APPEND ASMTK_PRIVATE_CFLAGS -MP # [+] Multi-Process Compilation. -GR- # [-] Runtime type information. -GF # [+] Eliminate duplicate strings. -Zc:inline # [+] Remove unreferenced COMDAT. -Zc:strictStrings # [+] Strict const qualification of string literals. -Zc:threadSafeInit- # [-] Thread-safe statics. -W4) # [+] Warning level 4. list(APPEND ASMTK_PRIVATE_CFLAGS_DBG -GS) # [+] Buffer security-check. list(APPEND ASMTK_PRIVATE_CFLAGS_REL -GS- # [-] Buffer security-check. -O2 # [+] Favor speed over size. -Oi) # [+] Generate intrinsic functions. elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|AppleClang)$") list(APPEND ASMTK_PRIVATE_CFLAGS -Wall -Wextra -Wconversion) list(APPEND ASMTK_PRIVATE_CFLAGS -fno-math-errno -fno-threadsafe-statics) list(APPEND ASMTK_PRIVATE_CFLAGS_REL -O2) endif() endif() ``` -------------------------------- ### Deprecated Option Handling for AsmTK Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Handles deprecated build options like ASMTK_BUILD_EMBED and ASMTK_BUILD_STATIC, providing deprecation messages and mapping them to the current options ASMTK_EMBED and ASMTK_STATIC. ```cmake if (DEFINED ASMTK_BUILD_EMBED) message(DEPRECATION "ASMTK_BUILD_EMBED is deprecated, use ASMTK_EMBED") set(ASMTK_EMBED "${ASMTK_BUILD_EMBED}") endif() if (DEFINED ASMTK_BUILD_STATIC) message(DEPRECATION "ASMTK_BUILD_STATIC is deprecated, use ASMTK_STATIC") set(ASMTK_STATIC "${ASMTK_BUILD_STATIC}") endif() ``` -------------------------------- ### AsmJIT Dependency Handling Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Conditionally includes the AsmJIT CMakeLists.txt or uses find_package(asmjit CONFIG REQUIRED) based on the ASMJIT_EXTERNAL variable. If AsmJIT is not external, it appends AsmJIT's CFLAGS to AsmTK's private CFLAGS. ```cmake if (ASMJIT_EXTERNAL) find_package(asmjit CONFIG REQUIRED) else() include("${ASMJIT_DIR}/CMakeLists.txt") list(APPEND ASMTK_PRIVATE_CFLAGS ${ASMJIT_CFLAGS}) list(REMOVE_DUPLICATES ASMTK_PRIVATE_CFLAGS) endif() ``` -------------------------------- ### CMake Project Definition Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Defines the project name as 'asmtk' and specifies C++ as the language. It includes logic to prevent re-defining the project if it has already been created, which is useful for embedded projects. ```cmake if (NOT CMAKE_PROJECT_NAME OR "${CMAKE_PROJECT_NAME}" STREQUAL "asmtk") project(asmtk CXX) endif() ``` -------------------------------- ### AsmTK Static Flag Inclusion Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Appends the '-DASMTK_STATIC' preprocessor definition to ASMTK_CFLAGS and ASMTK_PRIVATE_CFLAGS if AsmTK is configured to be embedded or static. This ensures the correct build configuration is reflected during compilation. ```cmake if (ASMTK_EMBED OR ASMTK_STATIC) List(APPEND ASMTK_CFLAGS "-DASMTK_STATIC") List(APPEND ASMTK_PRIVATE_CFLAGS "-DASMTK_STATIC") endif() ``` -------------------------------- ### AsmTK Target Type Determination Source: https://github.com/asmjit/asmtk/blob/master/CMakeLists.txt Determines the AsmTK target type (EMBED, STATIC, or SHARED) based on the configuration variables ASMTK_EMBED and ASMTK_STATIC. This sets the ASMTK_TARGET_TYPE variable accordingly. ```cmake if (ASMTK_EMBED) set(ASMTK_TARGET_TYPE "EMBED") elseif (ASMTK_STATIC) set(ASMTK_TARGET_TYPE "STATIC") else() set(ASMTK_TARGET_TYPE "SHARED") endif() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.