From 30d0787a62c3541c3b03fccadd2331cdd554d76c Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Thu, 23 Mar 2017 12:06:29 -0700 Subject: [PATCH] so that's how libraries work --- CMakeLists.txt | 6 ++++-- include/bottles.hpp | 6 +++++- include/dll.hpp | 24 ++++++++++++++++++++++++ src/{ => bottles}/bottles.cpp | 10 ++++++++-- src/cellar.cpp | 4 ++++ 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 include/dll.hpp rename src/{ => bottles}/bottles.cpp (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 09fcda9..4cc4414 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,9 +35,11 @@ foreach(cogfile ${cogfiles}) set(coggedfiles ${coggedfiles} "${thisfile}") endforeach(cogfile) +add_library(bottles SHARED ${src}/bottles/bottles.cpp) + add_custom_target(cog ALL DEPENDS ${coggedfiles}) add_executable(cellar ${src}/cellar.cpp ${src}/commands.cpp ${src}/fs.cpp - ${src}/bottles.cpp ${src}/version.cpp) + ${src}/version.cpp) add_dependencies(cellar cog) -target_link_libraries(cellar ${Boost_LIBRARIES}) +target_link_libraries(cellar ${Boost_LIBRARIES} bottles) diff --git a/include/bottles.hpp b/include/bottles.hpp index e5192ee..880866c 100644 --- a/include/bottles.hpp +++ b/include/bottles.hpp @@ -5,6 +5,7 @@ #include #include +#include "commands.hpp" #include "json.hpp" using namespace std; @@ -27,8 +28,11 @@ namespace cellar { string canonical_path; Bottle(); }; - map get_bottles(); + extern map get_bottles(); } + namespace commands { + extern map bottles_commands(); + } } #endif // __BOTTLES_HPP diff --git a/include/dll.hpp b/include/dll.hpp new file mode 100644 index 0000000..7280225 --- /dev/null +++ b/include/dll.hpp @@ -0,0 +1,24 @@ +#if defined _WIN32 || defined __CYGWIN__ + #ifdef BUILDING_DLL + #ifdef __GNUC__ + #define DLL_PUBLIC __attribute__ ((dllexport)) + #else + #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax. + #endif + #else + #ifdef __GNUC__ + #define DLL_PUBLIC __attribute__ ((dllimport)) + #else + #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax. + #endif + #endif + #define DLL_LOCAL +#else + #if __GNUC__ >= 4 + #define DLL_PUBLIC __attribute__ ((visibility ("default"))) + #define DLL_LOCAL __attribute__ ((visibility ("hidden"))) + #else + #define DLL_PUBLIC + #define DLL_LOCAL + #endif +#endif diff --git a/src/bottles.cpp b/src/bottles/bottles.cpp similarity index 88% rename from src/bottles.cpp rename to src/bottles/bottles.cpp index 032bd2d..2a4d6e2 100644 --- a/src/bottles.cpp +++ b/src/bottles/bottles.cpp @@ -11,11 +11,13 @@ #include "bottles.hpp" #include "commands.hpp" +#include "dll.hpp" #include "fs.hpp" using namespace std; using namespace cellar::bottles; +using CommandFunction = cellar::commands::CommandFunction; using json = nlohmann::json; Bottle::Bottle() { @@ -25,7 +27,7 @@ Bottle::Bottle() { type = bottle_anonymous; } -map cellar::bottles::get_bottles() { +DLL_PUBLIC map cellar::bottles::get_bottles() { map result; string homepath = getenv("HOME"); @@ -95,4 +97,8 @@ void print_bottles(int argc, char** argv) { cout << endl; } } -cellar::commands::CommandFunction listcmd = cellar::commands::command_map["list"] = &print_bottles; +DLL_PUBLIC map cellar::commands::bottles_commands() { + map result; + result.insert(pair("list", &print_bottles)); + return result; +} diff --git a/src/cellar.cpp b/src/cellar.cpp index 464a333..93a9e51 100644 --- a/src/cellar.cpp +++ b/src/cellar.cpp @@ -37,6 +37,10 @@ int main(int argc, char* argv[]) { cmdparse.parse(argc, argv); + for (auto item : commands::bottles_commands()) { + commands::command_map[item.first] = item.second; + } + string usercmd = command.getValue(); if (commands::command_map.count(usercmd) > 0) { commands::command_map[usercmd](argc, argv);