corrosion icon indicating copy to clipboard operation
corrosion copied to clipboard

Doesn't compile if not linked to anything

Open elichai opened this issue 2 years ago • 3 comments

Just doing:

cmake_minimum_required(VERSION 3.4.1)

include(FetchContent)
FetchContent_Declare(
    Corrosion
    GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
    GIT_TAG origin/master # TODO: Lock to a specific version in the future
)
FetchContent_MakeAvailable(Corrosion)

set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON")
set (CMAKE_CXX_STANDARD 11)

corrosion_import_crate(MANIFEST_PATH ../rust/Cargo.toml)

Doesn't compile anything. I'm using this as part of Android's build system, so all I need is the rust code compiled to a shared library, in the meantime adding an empty dummy.c file and adding these lines solved it:

add_library(example
            SHARED
            dummy.c
)
target_link_libraries(example PUBLIC rust)

(P.S. Thank you for this library!! it makes it so much easier to integrate rust into a multiplatform build system)

elichai avatar Apr 20 '22 15:04 elichai

Instead of adding the dummy example library, you should also be able to to use add_dependencies: add_dependencies(your_android_target rust_target).

I think this is basically just a side effect of corrosion exposing the main target as an INTERFACE library (and I don't fully understand the reason for that).

I'm using this as part of Android's build system [...]

Out of curiosity, which CMake version are you using? Is the Android NDK comptabile with basically any newer CMake version, or are there some known restrictions?

jschwe avatar Apr 20 '22 16:04 jschwe

@jschwe

Instead of adding the dummy example library, you should also be able to to use add_dependencies: add_dependencies(your_android_target rust_target).

So, there's not really an "android target" in the cmake, the cmake just creates the shared object, which in my case I want it to be the rust code, so rust is the "final" target here.

Out of curiosity, which CMake version are you using? Is the Android NDK comptabile with basically any newer CMake version, or are there some known restrictions?

AFAIU android supports any new CMake version

elichai avatar Apr 26 '22 08:04 elichai

If you are not doing anything else with the rust library, it would probably be easier to just add cargo build <options> as a custom command to CMake and copy the built library wherever you need it. I don't think Corrosion offers much of a benefit if you are not actively linking to or from foreign code built with CMake.

If you do want to you Corrosion, then I guess creating a dummy library is all you can do for now.

AFAIU android supports any new CMake version

Thanks! That's good to hear.

jschwe avatar Apr 30 '22 11:04 jschwe