How Can I Have Multiple Executables In A Single Swift 3 Package? - swift3

I'm doing Advent Of Code and have each day's project in one Swift package so I can easily share pieces between them. Each day produces its own executable, day01, day02, and so on.
My package is laid out like so:
$ tree
.
├── Package.swift
├── Sources
│   ├── SomethingShared
│   │   └── Shared.swift
│   ├── day01
│   │   └── main.swift
│   └── day02
│   └── main.swift
└── Tests
├── SomethingSharedTests
│   └── SharedTest.swift
├── day01Tests
│   └── day01Test.swift
└── day02Tests
└── day02Test.swift
This builds fine, but the tests won't link with more than one main.swift.
$ swift test
Compile Swift Module 'SomethingShared' (1 sources)
Compile Swift Module 'day02Tests' (1 sources)
Compile Swift Module 'day01Tests' (1 sources)
Linking ./.build/debug/day01
Linking ./.build/debug/day02
Compile Swift Module 'SomethingSharedTests' (1 sources)
Linking ./.build/debug/AdventOfCode2016PackageTests.xctest/Contents/MacOS/AdventOfCode2016PackageTests
duplicate symbol _main in:
/Users/schwern/tmp/foo/.build/debug/day01.build/main.swift.o
/Users/schwern/tmp/foo/.build/debug/day02.build/main.swift.o
ld: 1 duplicate symbol for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
error: exit(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/schwern/tmp/foo/.build/debug.yaml test
It seems like it's trying to link the main functions into one library.
How can I prevent this? Alternatively, is there a better way to accomplish this?
Here is a zip of the test project if you want to look at the contents, there isn't much there. Here is my real project if you'd like to see what I'm doing.

Related

How to configure an AzerothCore module to link with the external library?

I am developing a module, which depends on another library(wasmtime). I put files into:
modules/mod_wasm/src/include - header files, and
modules/mod_mine/src/lib/libwasmtime.a - the compiled library.
The problem which I faced is that when I compile the acore server with
./acore.sh compiler all
it gives me the error:
[100%] Linking CXX executable worldserver
/usr/bin/ld: ../../../modules/libmodules.a(ModWasm.cpp.o): in function `readWasmFile(char const*)':
ModWasm.cpp:(.text+0x63): undefined reference to `wasm_byte_vec_new_uninitialized'
/usr/bin/ld: ModWasm.cpp:(.text+0xce): undefined reference to `wasm_byte_vec_delete'
The question is it required somehow add to a config that library? If yes, then how to do that?
I was testing my code in simple main.cpp file and it was working with options like "-L${workspaceFolder}/lib" and "-lwasmtime".
Maybe, these options are also required for my module?
Here is a link to azerothcore project which I use.
my module locates in modules/mod-wasm folder
azerothcore-wotlk/modules ‹master*› » tree -L 3 mod-wasm
mod-wasm
├── CMakeLists.txt
├── LICENSE
├── Makefile
├── README.md
├── conf
│   ├── conf.sh.dist
│   └── wasm.conf.dist
├── include.sh
├── mod-wasm.cmake
├── setup_git_commit_template.sh
├── src
│   ├── ModWasm.cpp
│   ├── include
│   │   ├── doc-wasm.h
│   │   ├── wasi.h
│   │   ├── wasm.h
│   │   ├── wasmtime
│   │   ├── wasmtime.h
│   │   └── wasmtime.hh
│   ├── lib
│   │   ├── libwasmtime.a
│   │   └── libwasmtime.so
│   └── wasm_loader.cpp
└── wasm_modules
└── rust_wasm_app.wasm
As I understood from the logs what I see and because CMakeList.txt exists in modules folder, the project considers the folder as module. Which in its turn scans subdirs for *.cmake files and configures the project.
The question now is how to properly configure my module to show that it contains the compiled library wasmtime inside src/lib folder?
As I understood, I could use target_link_libraries, but it requires a target name, and I have no idea what it should be and where I can take it.
At the end, I was able to find an answer with try and catch.
Azerothcore modules supports modname.cmake file to be run when configure libmodules.a which contains all extra modules(if I understood it correctly.
this is part of modules/CMakeFiles.txt
# Enables Devs to Include a cmake file in their module that will get run inline with the config.
foreach(SOURCE_MODULE ${MODULES_MODULE_LIST})
message("SOURCE_MODULE: ${SOURCE_MODULE}")
include("${CMAKE_SOURCE_DIR}/modules/${SOURCE_MODULE}/${SOURCE_MODULE}.cmake" OPTIONAL)
endforeach()
here I have my dirty cmake file which allow me to compile the server
set(WASM_MODULE_DIR ${CMAKE_SOURCE_DIR}/modules/${SOURCE_MODULE})
set(WASM_MODULE_SRC_DIR ${WASM_MODULE_DIR}/src)
message("--------------------->>>>> APPLICATION_NAME : ${APPLICATION_NAME}")
message("--------------------->>>>> APP_PROJECT_NAME : ${APP_PROJECT_NAME}")
message("--------------------->>>>> SOURCE_MODULE : ${SOURCE_MODULE}")
message("--------------------->>>>> WASM_MODULE_DIR : ${WASM_MODULE_DIR}")
message("--------------------->>>>> WASM_MODULE_SRC_DIR : ${WASM_MODULE_SRC_DIR}")
# include wasmtime
target_include_directories(modules PUBLIC ${WASM_MODULE_SRC_DIR}/include)
target_link_directories(modules PUBLIC ${WASM_MODULE_SRC_DIR}/lib)
find_library(LIBWASMTIME_TO_INCLUDE NAMES wasmtime PATHS ${WASM_MODULE_SRC_DIR}/lib REQUIRED)
message("--------------------->>>>>>>>> LIBWASMTIME_TO_INCLUDE: ${LIBWASMTIME_TO_INCLUDE}")
target_link_libraries(modules PUBLIC wasmtime)
So, it compiles now.
But I have next problem, which I am trying to resolve. but this is another story.
Thank you all for the help

How to specify gcm.cache location using g++ & CMake

I am currently working on a project which requires C++20 (g++ v11) features and CMake. The project tree is similar to the following one:
- Top level
- src
- IO
- IO.cpp
- CMakeLists.txt
- main.cpp
- CMakeLists.txt
CMake compiles IO module without any problem but It generates gcm.cache folder in a following way:
- build
- Some other CMake files and folders
- bin
- lib
- src
- IO
- gcm.cache
- IO.gcm
Therefore, g++ can not find gcm.cache folder and gives me this error:
IO: error: failed to read compiled module: No such file or directory
IO: note: compiled module file is 'gcm.cache/IO.gcm'
IO: note: imports must be built before being imported
IO: fatal error: returning to the gate for a mechanical issue
I would be grateful if anyone tell me that there is a way to specify gcm.cache locations using CMake or force CMake to search gcm files recursively or tell it to create a top level gcm.cache and store everything inside of it. I can not find any answer on anywhere since C++20 documentations are terrible. Thanks in advance...
I have experienced the exact same issue, and without actually discovering a solution have found a workaround. Complete code found here.
In short, I create a symbolic link such that subprojects are all using the gcm.cache/ directory located in the root directory of the project. Create a symlink like so:
ln -fs ../gcm.cache gcm.cache
This is the directory tree of the project:
.
├── engine
│   ├── core
│   │   └── types.cpp
│   ├── engine.cpp
│   ├── gcm.cache -> ../gcm.cache
│   ├── Makefile
│   └── memory
├── gcm.cache
├── init.sh
├── Makefile
└── testgame
├── gamelib.cpp
├── gcm.cache -> ../gcm.cache
├── Makefile
└── test.cpp
So when gcc builds the engine and testgame projects it actually uses the gcm.cache/ from the root directory. Until something better comes along this is my go-to method.

Qt proper project structure for testing

I am currently in the process of setting up QTests for my Qt project and I am struggling a bit to come up with a nice structure which fullfills my needs.
Atm the project looks like this:
.
├── keygen
│   ├── main.cpp
│   ├── tests
│   │   ├── passkeygen_tests.cpp
│   │   └── tests.pri
├── common.pri
├── MainApp
│   ├── mainapp.pro
│   ├── main.cpp
├── TestProject.pro
├── lib_utils
│   ├── utils.cpp
│   ├── lib_utils.pro
The TestProject is a subproject pro file containing info about keygen(standalone executable), MainApp and libutils (used by MainApp). As you can see I already added tests for the keygen according to what I saw in this example https://github.com/kelvins/CodeCoverageExampleQt/tree/master/CodeCoverageExample because I want to see the testcoverage of my subprojects. So the files look like this:
keygen.pro:
include($$PWD/../common.pri)
QT += core dbus testlib
TEMPLATE = app
DESTDIR = $${OUT_PWD}/bin
TARGET = keygen
include($$PWD/tests/tests.pri)
QMAKE_CXXFLAGS += --coverage
QMAKE_LFLAGS += --coverage
tests.pri:
SOURCES += $$PWD/passkeygen_tests.cpp
passkeygen_tests.cpp:
#include <QtTest>
#include "passkeygenerator.h"
class PasskeygenTester : public QObject
{
Q_OBJECT
private:
PasskeyGenerator passkeygen;
private slots:
void checkGeneration();
void checkGeneration_data();
};
//Implementations
static PasskeygenTester passkeygenInstance;
#include "passkeygen_tests.moc"
This works in the sense that when I compile the project I see the *.gcno files are generated, but this way I can not execute the tests standalone to get the actual coverage. So my question is what is the best way to do generate gcov information and have standalone executable tests? The link from earlier only cared about executing the test not runnability of the actual project.
I created a sample project which highlights the struggle I am facing especially when it comes to tests for a library:https://github.com/faxe1008/TestCoverageStructureTest

Linking static libraries in CMake Project - Undefined Reference

I'm trying to refactor some classes from a large main file into separate header and cpp files and am getting undefined reference errors at link time.
I've got a project that looks like this:
├── CMakeLists.txt
├── data
│   └── ICING BE SI Data.csv
├── gcc
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── lib
│   ├── Makefile
│   ├── src
│   └── tmp
├── include
│   ├── Interpolator.hpp
│   ├── InverseCDFProcess.hpp
│   └── XYParser.hpp
├── lib
│   ├── CMakeLists.txt
│   ├── Interpolator.cpp
│   ├── InverseCDFProcess.cpp
│   └── XYParser.cpp
└── test
└── test_icing.cpp
The project has a few classes, Interpolator and InverseCDFProcess, which I recently moved from the main executable file, test_icing.cpp to their own .cpp and .hpp files, located within the lib and include directories, respectively.
Since the classes do depend on each other (InverseCDFProcess needs Interpolator, which in turn needs a function in XYParser.cpp), I decided to build them as static libraries that then get linked into the main executable at compile time.
They're built like so:
add_library(xyparser STATIC XYParser.cpp)
add_library(interpolator STATIC Interpolator.cpp)
add_library(inversecdf STATIC InverseCDFProcess.cpp)
I then link these libraries into my executable in the normal way:
include_directories(include)
link_directories(lib)
link_directories(include) # Do I need this?
add_executable(test_icing test/test_icing.cpp)
# ... some code adding an external library which works fine
target_link_libraries(test_icing inversecdf interpolator xyparser ${external_library_name})
This produces this link command:
/usr/bin/c++ CMakeFiles/test_icing.dir/test/test_icing.cpp.o -o test_icing -L/mnt/c/Users/foo/projects/chase-icing/lib -L/mnt/c/Users/foo/projects/chase-icing/include -L/mnt/c/Users/foo/projects/chas
e-icing/gcc/src/imtc-build/lib -Wl,-rpath,/mnt/c/Users/foo/projects/chase-icing/lib:/mnt/c/Users/foo/projects/chase-icing/include:/mnt/c/Users/foo/projects/chase-icing/gcc/src/imtc-build/lib lib/libinversecdf
.a lib/libinterpolator.a lib/libxyparser.a -limt
At this point the compilation stop with the error:
/mnt/c/Users/foo/projects/chase-icing/test/test_icing.cpp:(.text+0xcca): undefined reference to `Interpolator<double>::Interpolator(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > co
nst&)'
/mnt/c/Users/foo/projects/chase-icing/test/test_icing.cpp:(.text+0xd4c): undefined reference to `Interpolator<double>::set_bounds(std::pair<double, double> const&)'
/mnt/c/Users/foo/projects/chase-icing/test/test_icing.cpp:(.text+0xd99): undefined reference to `InverseCDFProcess<double>::InverseCDFProcess(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<
char> > const&)'
/mnt/c/Users/foo/projects/chase-icing/test/test_icing.cpp:(.text+0xdd9): undefined reference to `InverseCDFProcess<double>::generate()'
It doesn't matter if the libraries are built STATIC or SHARED. The undefined reference error still happens.
My question is this: am I missing some extern or similar in my class definitions or implementations? Why is this relatively straightforward refactoring resulting in undefined references? Is my link directory incorrect? Should it refer to build directories?
Any help is appreciated.
Gonna go ahead and answer this for future people:
The solution is embedded in the error messages:
/mnt/c/Users/foo/projects/chase-icing/test/test_icing.cpp:(.text+0xdd9): undefined reference to `InverseCDFProcess<double>::generate()'
This error shows that the classes are templated. The problem is that I placed the implementations of these templates in .cpp files, as seen here:
├── include
│ ├── Interpolator.hpp
│ ├── InverseCDFProcess.hpp
│ └── XYParser.hpp
├── lib
│ ├── CMakeLists.txt
│ ├── Interpolator.cpp
│ ├── InverseCDFProcess.cpp
│ └── XYParser.cpp
Templates need to contain the full implementation in the header files. So, the good news is that I don't need those libraries in the first place. Just #include "Interpolator.hpp etc and it should work as expected!
The reason templates need the implementation is seen here: Why can templates only be implemented in the header file?

Libclang don't follow include statement

I'm trying to use Libclang to programatically analyse the Opencv library, but when I try to import the main header from Opencv opencv.hpp, libclang won't follow the path.
Previously, it was reading everything quite beautifully, but then I figured it was following my $PATH's headers, and I want it to follow these specifics ones.
opencv.hpp is a file containing lots of #include statements like so:
#include "core/core_c.h"
#include "core/core.hpp"
#include "flann/miniflann.hpp"
// ... and so on
but, when I try to open it with libclang, or either clang ./opencv.hpp, it won't follow:
clang ./Header_Example/opencv.hpp
./Header_Example/opencv.hpp:46:10: fatal error: 'core/core_c.h' file not found
#include "core/core_c.h"
^
1 error generated.
but I'm sure it is on the right directory (a bit of my tree output):
── Header_Example
│   ├── opencv.hpp
│   ├── opencv2
│   │   ├── # more directories
│   │   ├── core
│   │   │   ├── affine.hpp
│   │   │   ├── core.hpp
│   │   │   ├── core_c.h
│   │   │   ├── types_c.h
│   │   │   ├── version.hpp
│   │   │   └── wimage.hpp
maybe I'm not using the right clang parameters?
A bit of context: I want to analyse Opencv types, classes and functions, this info is present on the headers, so I don't think I would need the full library to read the code. When I tried to use the full library I found myself in trouble. I need to use the iOS compilation(?)version(?) of the library, hence I copy/pasted the headers from the compiled version into this working directory (sorry? :) )
EDIT 1: It may seem odd my directories having this opencv2/, but if I remove the headers from inside of it, clang will complain 'bout why aren't they there: fatal error: 'opencv2/core/types_c.h' file not found
I simply had to add -I. before adding -I./opencv2 because I had one file one the same directory as opencv.hpp. Gosh I feel so stupid for missing this.