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

Add custom command in cmake generator #1735

Closed
TNtube opened this issue Oct 8, 2021 · 12 comments
Closed

Add custom command in cmake generator #1735

TNtube opened this issue Oct 8, 2021 · 12 comments

Comments

@TNtube
Copy link

TNtube commented Oct 8, 2021

A command to copy folders/files that can be translated automaticaly to cmake

I use CLion, and one of my projects require the use of an asset folder. I know how to make a simple copy cmd in xmake using lua, but when i generate a cmake from this, that cmd is just ignored and the app just crash at runtime unless i manually copy files.

I actually use this method :

    after_build(function (target)
            os.cp("SandBox/assets", "build/" .. outputdir .. "/SandBox/bin")
        end)

A good solution I thought about is to maybe create a xmake command to directly copy files/folder afterbuild, that can be automatically translated to cmake.

Thank you for your job on this project, I hope this will be considerated.
If there is already a solution to this problem, I apologize.

@TNtube
Copy link
Author

TNtube commented Oct 8, 2021

(i forgot to precise that this is a feature request)

@waruqi
Copy link
Member

waruqi commented Oct 8, 2021

Currently only rule+after_buildcmd and on_buildcmd_file support build commands. I remember that the makefile and compile_commands generators already support them, but the cmake generator does not yet support them.

I will consider this feature later, please be patient.

@xq114
Copy link
Contributor

xq114 commented Oct 10, 2021

https://cmake.org/cmake/help/latest/command/add_custom_command.html Something may help. For custom rules we can extract the code snippets and use add_custom_command to invoke xmake l <snippet>.lua in CMakeLists.txt

@waruqi waruqi added this to the v2.5.9 milestone Oct 11, 2021
@waruqi waruqi changed the title Add a command to copy files/folders Add custom command in cmake generator Oct 25, 2021
@waruqi
Copy link
Member

waruqi commented Oct 28, 2021

https://cmake.org/cmake/help/latest/command/add_custom_command.html

ADD_CUSTOM_COMMAND and PRE_BUILD did not work as I expected.

https://gitlab.kitware.com/cmake/cmake/-/issues/17802

[ 50%] Building C object CMakeFiles/test.dir/src/main.c.o
[100%] Linking C executable build/macosx/x86_64/debug/test
do pre_build

@waruqi
Copy link
Member

waruqi commented Oct 28, 2021

I have supported it.

we need use rule with batchcmds to support it.

rule:

  • before/after_buildcmd
  • before/after_buildcmd_file
  • before/after_buildcmd_files
rule("foo")
    after_buildcmd(function (target, batchcmds, opt)
        batchcmds:show("hello xmake!")
        batchcmds:cp("xmake.lua", "/tmp/")
        -- batchcmds:execv("echo", {"hello", "world!"})
        -- batchcmds:runv("echo", {"hello", "world!"})
    end)

target("test")
    set_kind("binary")
    add_rules("foo")
    add_files("src/*.c")

CMakelists.txt

# this is the build file for project 
# it is autogenerated by the xmake build system.
# do not edit by hand.

# project
cmake_minimum_required(VERSION 3.13.0)
project(test LANGUAGES C CXX ASM)

# target
add_executable(test "")
set_target_properties(test PROPERTIES OUTPUT_NAME "test")
set_target_properties(test PROPERTIES RUNTIME_OUTPUT_DIRECTORY "build/macosx/x86_64/release")
target_compile_options(test PRIVATE
    $<$<COMPILE_LANGUAGE:C>:-arch>
    $<$<COMPILE_LANGUAGE:CXX>:-arch>
    $<$<COMPILE_LANGUAGE:C>:x86_64>
    $<$<COMPILE_LANGUAGE:CXX>:x86_64>
    $<$<COMPILE_LANGUAGE:C>:-mmacosx-version-min=10.15>
    $<$<COMPILE_LANGUAGE:CXX>:-mmacosx-version-min=10.15>
    $<$<COMPILE_LANGUAGE:C>:-isysroot>
    $<$<COMPILE_LANGUAGE:CXX>:-isysroot>
    $<$<COMPILE_LANGUAGE:C>:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk>
    $<$<COMPILE_LANGUAGE:CXX>:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk>
)
target_link_libraries(test PRIVATE
    z
)
target_link_options(test PRIVATE
    -mmacosx-version-min=10.15
    -stdlib=libc++
)
add_custom_command(TARGET test
    POST_BUILD
    COMMAND echo hello xmake!
    VERBATIM
)
add_custom_command(TARGET test
    POST_BUILD
    COMMAND cp xmake.lua /tmp/
    VERBATIM
)
target_sources(test PRIVATE
    src/main.c
)

@TNtube
Copy link
Author

TNtube commented Oct 28, 2021

Thank you very much, you are incredibly efficient !

@waruqi
Copy link
Member

waruqi commented Oct 28, 2021

We can also run a full xmake script file

rule("foo")
    after_buildcmd(function (target, batchcmds, opt)
        batchcmds:execv("xmake", {"lua", path.join(os.scriptdir(), "myscript.lua")})
    end)

@waruqi waruqi closed this as completed Oct 28, 2021
@xq114
Copy link
Contributor

xq114 commented Oct 28, 2021

That's really nice! However I think it's better using a platform independent command other than cp, which is only available on UNIX-like platforms. cmake -E copy and xmake l os.cp are both better choices

@waruqi
Copy link
Member

waruqi commented Oct 28, 2021

xmake l os.cp

This will additionally rely on xmake, which is inconvenient for external distribution. But cmake -E copy can be considered

@xq114
Copy link
Contributor

xq114 commented Oct 28, 2021

This will additionally rely on xmake, which is inconvenient for external distribution. But cmake -E copy can be considered

To make it more robust ${CMAKE_COMMAND} -E copy should be used since generally cmake isn't required to be in $PATH. see https://cmake.org/cmake/help/latest/variable/CMAKE_COMMAND.html

@waruqi
Copy link
Member

waruqi commented Oct 28, 2021

Thanks, I have improved it.

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

No branches or pull requests

3 participants