Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xmake support #2782

Open
tatsumoto-ren opened this issue Dec 14, 2023 · 8 comments
Open

Xmake support #2782

tatsumoto-ren opened this issue Dec 14, 2023 · 8 comments

Comments

@tatsumoto-ren
Copy link

tatsumoto-ren commented Dec 14, 2023

I was able to successfully use Catch2 v2 with Xmake by simply adding #define CATCH_CONFIG_MAIN and #include <catch2/catch.hpp> to the tests cpp file. However, I couldn't figure out how to make Catch2 v3 work with Xmake yet. I get undefined reference to 'main'.

There's a page about Cmake integration but unfortunately there's no page about Xmake integration. It would be very helpful if such a file existed.

I'm guessing I have to link with Catch2Main but adding add_ldflags("-lCatch2Main") didn't help so I'm currently out of ideas.

@waruqi
Copy link

waruqi commented Dec 14, 2023

try

add_rules("mode.debug", "mode.release")

add_requires("catch2 3.x", {system = false})

target("catch2_test")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("catch2")
    set_languages("c++17")
ruki-2:catch2_test ruki$ xmake
checking for platform ... macosx
checking for architecture ... x86_64
checking for Xcode directory ... /Applications/Xcode.app
checking for SDK version of Xcode for macosx (x86_64) ... 14.0
checking for Minimal target version of Xcode for macosx (x86_64) ... 14.0
note: install or modify (m) these packages (pass -y to skip confirm)?
in xmake-repo:
  -> catch2 v3.4.0
please input: y (y/n/m)

  => install catch2 v3.4.0 .. ok
[100%]: build ok, spent 0.071s

ruki-2:catch2_test ruki$ xmake run
Randomness seeded to: 3680645800
===============================================================================
All tests passed (4 assertions in 1 test case)

@tatsumoto-ren
Copy link
Author

tatsumoto-ren commented Dec 14, 2023

@waruqi thank you.

After some trial and error I got Catch2 working with these settings:

  1. In a cpp file, change #include <catch2/catch.hpp> to #include <catch2/catch_test_macros.hpp> and remove #define CATCH_CONFIG_MAIN
  2. In xmake.lua :
    add_requires("catch2 3", { system = false, configs = {components = {"main"}, }})
    
    As I understand it, system = false is required to pull the package from xrepo. And without the main component build will fail with "undefined reference to 'main'." But people who use their own main don't need to add the component.
  3. In target("tests"):
    remove_files("src/main.cpp")
    
    This removes the main function used in a regular build.

I think it would be great to add a new xmake-integration.md file to this directory to help new users set up Catch2 in the future. @horenmar what do you think?

@waruqi
Copy link

waruqi commented Dec 15, 2023

As I understand it, system = false is required to pull the package from xrepo. And without the main component build will fail with "undefined reference to 'main'." But people who use their own main don't need to add the component.

main component always will be enabled for catch2 3.x, you need not configure it.

https://github.com/xmake-io/xmake-repo/blob/cb9b8b7dbdca624419dce6c425cecaf66d2abc3b/packages/c/catch2/xmake.lua#L35

please remove configs = {components = {"main"}, }, it's incorrect.

@tatsumoto-ren
Copy link
Author

main component always will be enabled for catch2 3.x, you need not configure it.

My bad, didn't know about this.

please remove configs = {components = {"main"}, }, it's incorrect.

Removed. Yes, it works with just add_requires("catch2 3.x", { system = false }).

@waruqi
Copy link

waruqi commented Dec 15, 2023

main component always will be enabled for catch2 3.x, you need not configure it.

My bad, didn't know about this.

please remove configs = {components = {"main"}, }, it's incorrect.

Removed. Yes, it works with just add_requires("catch2 3.x", { system = false }).

if you are using brew on macOS, it should work now.

add_requires("catch2")
$ xrepo update-repo
$ xmake f -c
$ xmake

@tatsumoto-ren
Copy link
Author

I'm running Arch Linux, and it seems that the catch2 package from the official repositories (installed with pacman -S catch2) doesn't work correctly. If I remove { system = false } and just have add_requires("catch2") in xmake.lua instead, I get undefined reference to main as before.

@waruqi
Copy link

waruqi commented Dec 15, 2023

I'm running Arch Linux, and it seems that the catch2 package from the official repositories (installed with pacman -S catch2) doesn't work correctly. If I remove { system = false } and just have add_requires("catch2") in xmake.lua instead, I get undefined reference to main as before.

I have improve this package, you can try it again.

add_requires("catch2")
xrepo update-repo
xmake f -cvD
xmake -rv
$ xmake -rv
checking for gcc ... /usr/sbin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for /usr/sbin/gcc ... ok
checking for flags (-fPIC) ... ok
checking for flags (-fvisibility-inlines-hidden) ... ok
checking for flags (-O3) ... ok
checking for flags (-DNDEBUG) ... ok
[ 25%]: cache compiling.release src/main.cpp
/usr/sbin/gcc -c -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -o build/.objs/test/linux/x86_64/release/src/main.cpp.o src/main.cpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
checking for g++ ... /usr/sbin/g++
checking for the linker (ld) ... g++
checking for /usr/sbin/g++ ... ok
checking for flags (-fPIC) ... ok
[ 50%]: linking.release test
/usr/sbin/g++ -o build/linux/x86_64/release/test build/.objs/test/linux/x86_64/release/src/main.cpp.o -m64 -s -lCatch2Main -lCatch2
[100%]: build ok, spent 1.506s

-lCatch2Main -lCatch2

@tatsumoto-ren
Copy link
Author

Yes, it seems to be working even without { system = false } now, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants