diff --git a/cmake/TaichiCore.cmake b/cmake/TaichiCore.cmake index 1b0e0ec537210..9e5d25ad35b26 100644 --- a/cmake/TaichiCore.cmake +++ b/cmake/TaichiCore.cmake @@ -74,20 +74,45 @@ if (TI_WITH_CC) list(APPEND TAICHI_CORE_SOURCE ${TAICHI_CC_SOURCE}) endif() -# We don't mean to create a dedicated library just for common. Instead, we are -# trying to identify, refactor and group the core files into a smaller lib. This -# lib should be mostly self-contained and completely Taichi focused, free from -# the "application" layer such as pybind11 or GUI. -file(GLOB TAICHI_TESTABLE_SRC "taichi/common/*.cpp" "taichi/common/*.h") +# This compiles all the libraries with -fPIC, which is critical to link a static +# library into a shared lib. +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# The short-term goal is to have a sub-library that is mostly Taichi-focused, +# free from the "application" layer such as pybind11 or GUI. At a minimum, we +# must decouple from pybind11/python-environment. This sub-lib will then be +# unit testtable. +# TODO(#2198): Long-term speaking, we should create a separate library for each +# sub-module. This way we can guarantee that the lib dependencies form a DAG. +file(GLOB TAICHI_TESTABLE_SRC + "taichi/common/*.cpp" + "taichi/common/*.h" + "taichi/ir/ir_builder.*" + "taichi/ir/ir.*" + "taichi/ir/offloaded_task_type.*" + "taichi/ir/snode_types.*" + "taichi/ir/snode.*" + "taichi/ir/statements.*" + "taichi/ir/type_factory.*" + "taichi/ir/type_utils.*" + "taichi/ir/type.*" + "taichi/transforms/statement_usage_replace.cpp" + "taichi/program/arch.*" + "taichi/program/compile_config.*" + "taichi/system/timer.*" + "taichi/system/profiler.*" +) + # TODO(#2196): Maybe we can do the following renaming in the end? # taichi_core --> taichi_pylib (this requires python-side refactoring...) # taichi_testable_lib --> taichi_core set(TAICHI_TESTABLE_LIB taichi_testable_lib) -add_library(${TAICHI_TESTABLE_LIB} OBJECT ${TAICHI_TESTABLE_SRC}) -set_property(TARGET ${TAICHI_TESTABLE_LIB} PROPERTY POSITION_INDEPENDENT_CODE ON) +add_library(${TAICHI_TESTABLE_LIB} STATIC ${TAICHI_TESTABLE_SRC}) list(REMOVE_ITEM TAICHI_CORE_SOURCE ${TAICHI_TESTABLE_SRC}) -add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_CORE_SOURCE} ${PROJECT_SOURCES} $) + +add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_CORE_SOURCE}) +target_link_libraries(${CORE_LIBRARY_NAME} ${TAICHI_TESTABLE_LIB}) if (APPLE) # Ask OS X to minic Linux dynamic linking behavior diff --git a/cmake/TaichiTests.cmake b/cmake/TaichiTests.cmake index e810dd116104b..3352ec67ab189 100644 --- a/cmake/TaichiTests.cmake +++ b/cmake/TaichiTests.cmake @@ -15,7 +15,8 @@ include_directories( ${PROJECT_SOURCE_DIR}, ) -add_executable(${TESTS_NAME} ${TAICHI_TESTS_SOURCE} $) +add_executable(${TESTS_NAME} ${TAICHI_TESTS_SOURCE}) +target_link_libraries(${TESTS_NAME} taichi_testable_lib) target_link_libraries(${TESTS_NAME} gtest_main) add_test(NAME ${TESTS_NAME} COMMAND ${TESTS_NAME}) diff --git a/tests/cpp_new/ir/ir_builder_test.cpp b/tests/cpp_new/ir/ir_builder_test.cpp new file mode 100644 index 0000000000000..53a20a0242b8a --- /dev/null +++ b/tests/cpp_new/ir/ir_builder_test.cpp @@ -0,0 +1,22 @@ +#include "gtest/gtest.h" + +#include "taichi/ir/ir_builder.h" +#include "taichi/ir/statements.h" + +namespace taichi { +namespace lang { + +TEST(IRBuilder, Basic) { + IRBuilder builder; + auto *lhs = builder.get_int32(40); + auto *rhs = builder.get_int32(2); + auto *add = builder.create_add(lhs, rhs); + ASSERT_TRUE(add->is()); + auto *addc = add->cast(); + EXPECT_EQ(addc->lhs, lhs); + EXPECT_EQ(addc->rhs, rhs); + EXPECT_EQ(addc->op_type, BinaryOpType::add); +} + +} // namespace lang +} // namespace taichi