Skip to content

Commit

Permalink
updated Makefile
Browse files Browse the repository at this point in the history
optional shared library support
  • Loading branch information
lostjared authored Oct 21, 2024
1 parent 3b390db commit f8e3587
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 32 deletions.
77 changes: 56 additions & 21 deletions compiler/ETL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,87 @@ endif()

# Option to enable SDL
option(WITH_SDL "Build with SDL support" OFF)

option(WITH_STATIC_SDL "build with static SDL" OFF)
# Add the core ETL library
add_library(etl STATIC ${CMAKE_SOURCE_DIR}/libetl/libetl.c ${CMAKE_SOURCE_DIR}/libetl/io.c)
target_include_directories(etl PUBLIC ${CMAKE_SOURCE_DIR}/libetl)

# Add the executable
add_executable(ETL_C
${CMAKE_SOURCE_DIR}/main.cpp
${CMAKE_SOURCE_DIR}/parser.cpp
${CMAKE_SOURCE_DIR}/test.cpp
${CMAKE_SOURCE_DIR}/ir.cpp
${CMAKE_SOURCE_DIR}/debug.cpp
${CMAKE_SOURCE_DIR}/interp.cpp
${CMAKE_SOURCE_DIR}/exec.cpp
${CMAKE_SOURCE_DIR}/embed_func.cpp
)

# Link scan_cxx library to ETL_C
target_include_directories(ETL_C PRIVATE ${scan_cxx_INCLUDE_DIRS})
target_link_libraries(ETL_C PRIVATE dl scan_cxx::cxx_scan)
target_compile_options(ETL_C PRIVATE -O2)

# SDL configuration if WITH_SDL is enabled
if(WITH_SDL)
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
add_compile_definitions(WITH_SDL)
# Create a separate SDL library

add_library(etl_sdl STATIC ${CMAKE_SOURCE_DIR}/libetl/sdl.c)
target_include_directories(etl_sdl PUBLIC ${SDL2_INCLUDE_DIRS} /usr/local/include/SDL2 /usr/include/SDL2)
target_link_libraries(etl_sdl PRIVATE ${SDL2_LIBRARIES})
install(TARGETS etl_sdl DESTINATION lib)


add_library(sdl_rt SHARED ${CMAKE_SOURCE_DIR}/libetl/sdl.c ${CMAKE_SOURCE_DIR}/libetl/shared/sdl/sdl.cpp)
target_include_directories(sdl_rt PRIVATE ${SDL2_INCLUDE_DIRS} /usr/local/include/SDL2 /usr/local/include /usr/include/SDL2)
target_include_directories(sdl_rt PRIVATE ${scan_cxx_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS} /usr/local/include/SDL2 /usr/local/include /usr/include/SDL2)
target_compile_options(sdl_rt PRIVATE -O2 -fPIC)
target_link_libraries(sdl_rt PRIVATE ${SDL2_LIBRARIES} SDL2_ttf::SDL2_ttf)
install(TARGETS sdl_rt DESTINATION /usr/local/lib)

# Conditionally install SDL-related files
install(FILES ${CMAKE_SOURCE_DIR}/libetl/sdl.e DESTINATION include/libetl)

if(WITH_STATIC_SDL)
add_compile_definitions(WITH_STATIC_SDL)
add_executable(ETL_C
${CMAKE_SOURCE_DIR}/main.cpp
${CMAKE_SOURCE_DIR}/parser.cpp
${CMAKE_SOURCE_DIR}/test.cpp
${CMAKE_SOURCE_DIR}/ir.cpp
${CMAKE_SOURCE_DIR}/debug.cpp
${CMAKE_SOURCE_DIR}/interp.cpp
${CMAKE_SOURCE_DIR}/exec.cpp
${CMAKE_SOURCE_DIR}/embed_func.cpp
${CMAKE_SOURCE_DIR}/libetl/sdl.c
${CMAKE_SOURCE_DIR}/libetl/shared/sdl/sdl.cpp
${CMAKE_SOURCE_DIR}/libetl/io.c
${CMAKE_SOURCE_DIR}/libetl/shared/io/io.cpp
)
target_include_directories(ETL_C PRIVATE ${scan_cxx_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS} /usr/local/include/SDL2 /usr/local/include /usr/include/SDL2)
target_link_libraries(ETL_C PRIVATE dl scan_cxx::cxx_scan ${SDL2_LIBRARIES} SDL2_ttf::SDL2_ttf )
target_compile_options(ETL_C PRIVATE -O2)
else()
add_executable(ETL_C
${CMAKE_SOURCE_DIR}/main.cpp
${CMAKE_SOURCE_DIR}/parser.cpp
${CMAKE_SOURCE_DIR}/test.cpp
${CMAKE_SOURCE_DIR}/ir.cpp
${CMAKE_SOURCE_DIR}/debug.cpp
${CMAKE_SOURCE_DIR}/interp.cpp
${CMAKE_SOURCE_DIR}/exec.cpp
${CMAKE_SOURCE_DIR}/embed_func.cpp
)
target_include_directories(ETL_C PRIVATE ${scan_cxx_INCLUDE_DIRS})
target_link_libraries(ETL_C PRIVATE dl scan_cxx::cxx_scan)
target_compile_options(ETL_C PRIVATE -O2)
endif()
else()
add_executable(ETL_C
${CMAKE_SOURCE_DIR}/main.cpp
${CMAKE_SOURCE_DIR}/parser.cpp
${CMAKE_SOURCE_DIR}/test.cpp
${CMAKE_SOURCE_DIR}/ir.cpp
${CMAKE_SOURCE_DIR}/debug.cpp
${CMAKE_SOURCE_DIR}/interp.cpp
${CMAKE_SOURCE_DIR}/exec.cpp
${CMAKE_SOURCE_DIR}/embed_func.cpp
)
target_include_directories(ETL_C PRIVATE ${scan_cxx_INCLUDE_DIRS})
target_link_libraries(ETL_C PRIVATE dl scan_cxx::cxx_scan)
target_compile_options(ETL_C PRIVATE -O2)
endif()

add_library(io_rt SHARED ${CMAKE_SOURCE_DIR}/libetl/io.c ${CMAKE_SOURCE_DIR}/libetl/shared/io/io.cpp)
target_include_directories(io_rt PRIVATE ${scan_cxx_INCLUDE_DIRS})
target_compile_options(io_rt PRIVATE -O2 -fPIC)
install(TARGETS io_rt DESTINATION /usr/local/lib)

Expand All @@ -72,6 +110,3 @@ install(PROGRAMS ${CMAKE_SOURCE_DIR}/etl DESTINATION bin)
install(PROGRAMS ${CMAKE_SOURCE_DIR}/etl-i DESTINATION bin)
install(FILES ${CMAKE_SOURCE_DIR}/etl-lib.txt DESTINATION "/usr/local/lib")
# Conditionally install SDL library if WITH_SDL is set
if(WITH_SDL)
install(TARGETS etl_sdl DESTINATION lib)
endif()
25 changes: 20 additions & 5 deletions compiler/ETL/embed_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include<fstream>
#include<dlfcn.h>



namespace lib {

void check_args(const std::string &n, const std::vector<interp::Var> &v, std::initializer_list<ast::VarType> args) {
Expand Down Expand Up @@ -306,13 +308,16 @@ namespace lib {
{"sprintf", func_sprintf}
};




std::vector<void *> shared_objects;

void addFunc(const char *src, void *ptr) {
func_table[std::string(src)] = (interp::FuncPtr)ptr;
}
void initSharedObject(const std::string &n) {

void initSharedObject(const std::string &n, const std::string &init_func) {
std::string path = n;
#ifdef __APPLE__
path += ".dylib";
Expand All @@ -333,7 +338,7 @@ namespace lib {
shared_objects.push_back(obj);
typedef void (*addFunction)(const char *src, void *ptr);
typedef void (*initTable)(addFunction);
initTable table = (initTable)dlsym(obj, "initTable");
initTable table = (initTable)dlsym(obj, init_func.c_str());
char *err = dlerror();
if(err) {
std::cerr << "ETL: Could not get Pointer to table function: " << err << "\n";
Expand All @@ -344,14 +349,24 @@ namespace lib {
table(addFunc);
}

#ifdef WITH_STATIC_SDL
void initStatic() {
libsdl_rt_initTable(addFunc);
libio_rt_initTable(addFunc);
}
#endif

void loadSharedObjects(const std::string &lib_path, const std::string &filename) {
std::fstream file;
file.open(filename, std::ios::in);
while(!file.eof()) {
std::string line;
std::getline(file, line);
if(file)
initSharedObject(lib_path + "/" + line);
if(file) {
std::string name = line.substr(line.rfind("/")+1);
std::string tname = name + "_initTable";
initSharedObject(lib_path + "/" + line, tname);
}
}
file.close();
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/ETL/embed_func.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

namespace lib {
extern std::unordered_map<std::string, interp::FuncPtr> func_table;
void initSharedObject(const std::string &n);
void initSharedObject(const std::string &name, const std::string &table_name);
void loadSharedObjects(const std::string &lib_path, const std::string &filename);
void releaseSharedObjects();
void initStatic();
}
typedef void (*addFunction)(const char *src, void *ptr);

#ifdef WITH_STATIC_SDL
extern "C" void libsdl_rt_initTable(addFunction f);
extern "C" void libio_rt_initTable(addFunction f);
#endif

#endif
14 changes: 9 additions & 5 deletions compiler/ETL/interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,23 @@ namespace interp {
return EXIT_SUCCESS;
}



void Interpreter::collectLabels(const ir::IRCode &code) {
int ip_id = 0;
std::string curFunc;
std::string curDefine;

lf_table.addFunction("printf", lib::func_table["printf"]);
lf_table.addFunction("sprintf", lib::func_table["sprintf"]);
std::cout << "ETL: Loading Shared Library Path: " << LIB_PATH << "\n";
#ifdef WITH_SDL
lib::initSharedObject(LIB_PATH + "/libsdl_rt");
#endif
#ifdef WITH_STATIC_SDL
lib::initStatic();
#else
#ifdef WITH_SDL
lib::initSharedObject(LIB_PATH + "/libsdl_rt", "libsdl_rt_initTable");
#endif
lib::loadSharedObjects(LIB_PATH, LIB_PATH + "/etl-lib.txt"); // config file

#endif

while(ip_id < code.size()) {
const auto instr = code[ip_id];
Expand Down

0 comments on commit f8e3587

Please sign in to comment.