premake-cmake icon indicating copy to clipboard operation
premake-cmake copied to clipboard

pm5example1 failure

Open arnholm opened this issue 3 years ago • 5 comments

I have written a Code::Blocks plugin to generate a premake5 script from a Code::Blocks workspace. There is an example with generated premake5 lua script at https://github.com/arnholm/pm5example1 . This example contains a static library, a shared library and a console application, all extremely small and simple.

It is possible to successfully build this example on linux or windows using premake5 generating makefiles or Visual Studion files:

premake5 --file=pm5example1_premake5.lua gmake2
premake5 --file=pm5example1_premake5.lua vs2019

Now I installed your premake-cmake under Kubuntu 20.04 to see if it would be possible to build the same via CMake, so I did

premake5 --file=pm5example1_premake5.lua cmake
Building configurations...
Running action 'cmake'...
Generated build/cmake/CMakeLists.txt...
Generated build/cmake/long/path/staticlib1/staticlib1.cmake...
Generated build/cmake/dynlib1/dynlib1.cmake...
Generated build/cmake/testconsole/testconsole.cmake...

and then

cd build/cmake
cmake .
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /ssd1/codeblocks/gtk2-unicode-3.0/plugin_dev/pm5example1/build/cmake

So far so good, but build fails

make
Scanning dependencies of target staticlib1
[ 16%] Building CXX object CMakeFiles/staticlib1.dir/ssd1/codeblocks/gtk2-unicode-3.0/plugin_dev/pm5example1/long/path/staticlib1/staticlib1_class.cpp.o
[ 33%] Linking CXX executable long/path/staticlib1/bin/debug/staticlib1
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/staticlib1.dir/build.make:84: long/path/staticlib1/bin/debug/staticlib1] Error 1
make[1]: *** [CMakeFiles/Makefile2:108: CMakeFiles/staticlib1.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

arnholm avatar Jul 28 '21 10:07 arnholm

undefined reference to `main' - seems like you're trying to build an executable instead of a static lib?

Enhex avatar Jul 28 '21 14:07 Enhex

It is premake-cmake that causes this. staticlib1 is a static lib without main. It works fine when building makefiles with premake5, but via cmake it fails. That's why I reported it here. The code and premake file is at https://github.com/arnholm/pm5example1

arnholm avatar Jul 28 '21 20:07 arnholm

i suggest trying to put some prints in the if-else's at https://github.com/Enhex/premake-cmake/blob/master/cmake_project.lua#L53 and seeing if it goes into the staticlib or executable path.

note that I don't use this project anymore and working on other stuff so i dont maintain it right now.

Enhex avatar Jul 28 '21 20:07 Enhex

Thanks for the tip. The reason for the failure is that prj.kind is nil in this case, because 'kind' is defined on premake config level and not on project level https://github.com/arnholm/pm5example1/blob/master/pm5example1_premake5.lua#L33

That looks like a bug in cmake-premake. A work-around in cmake_project.lua, i.e. test for prj.kind == nil and let the configs define it

function m.generate(prj)
    p.utf8()

    if prj.kind == nil then
        for cfg in project.eachconfig(prj) do
            prj.kind = cfg.kind
        end
    end

    if prj.kind == 'Utility' then
        return
    end
    ...

This fixes the immediate problem, but another problem appears when targetname is defined on config level in premake5. Unlike 'kind', 'targetname' is different for the different configs and premake-cmake doesn't handle that correctly. If I comment out 'targetname' in the premake5 file, it links properly.

So premake-cmake is almost working, but not quite.

EDIT: I should add that the file linked to above does not contain 'targetname' on config level, so it works with the fix mentioned. I observed the 'targetname' problem with a local modified copy

arnholm avatar Jul 29 '21 10:07 arnholm

I merged your PR, so closing this issue. If you'll need to use targetname in a similar manner make a PR and I'll merge it.

Enhex avatar Jul 29 '21 19:07 Enhex