COMBINE-lab / cuttlefish

Building the compacted de Bruijn graph efficiently from references or reads.
BSD 3-Clause "New" or "Revised" License
81 stars 9 forks source link

Patch step error while linking library with CMake #28

Open rlorigro opened 1 year ago

rlorigro commented 1 year ago

Hi,

I'm attempting to link your library with a combination of FetchContent and add_custom_target. Everything installs great, but when I link KMC (the one built by your CMakeLists) in order to satisfy your dependencies, I get this error during the build step:

[ 39%] Performing patch step for 'prj_kmc'
/bin/sh: 1: cannot open /home/ryan/code/test_dbg/patches/kmc_patch.diff: No such file

and I've noticed there is a relevant line in your CMake, which might explain this strange error: https://github.com/COMBINE-lab/cuttlefish/blob/30eef21275579e03efad81672e4198595bcb8dce/CMakeLists.txt#L190

I'm not familiar enough with CMake to understand this. Is there something I can do to work around it?

Here is my implementation:

message(STATUS "Fetching cuttlefish")

include(FetchContent)

message(STATUS "external: ${CMAKE_SOURCE_DIR}/external/")

FetchContent_Declare(
        project_cuttlefish
        URL      https://github.com/COMBINE-lab/cuttlefish/archive/refs/tags/v2.2.0.tar.gz
)

FetchContent_MakeAvailable(project_cuttlefish)

set(CUTTLEFISH_DIR _deps/project_cuttlefish-src)

add_custom_target(
        BUILD_CUTTLEFISH
        ALL
        WORKING_DIRECTORY ${CUTTLEFISH_DIR}
        COMMAND pwd
        COMMAND cmake -DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}/external/cuttlefish/ .
        COMMAND $(MAKE) install
)

message(STATUS "CUTTLEFISH_SOURCE_DIR: ${project_cuttlefish_SOURCE_DIR}")

# Ensure that main library has access to primary dependencies' and secondary dependencies' headers
include_directories(
        ${CMAKE_BINARY_DIR}/external/cuttlefish/include/cuttlefish/
        ${CMAKE_BINARY_DIR}/external/cuttlefish/include/
)

add_library(cuttlefish2 STATIC IMPORTED)

set_property(TARGET cuttlefish2
        PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/external/cuttlefish/lib/libcfcore_static.a)

add_library(kmc STATIC IMPORTED)

set_property(TARGET kmc
        PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/external/cuttlefish/lib/libkmc_core.a)

add_dependencies(cuttlefish2 BUILD_CUTTLEFISH)
add_dependencies(test_dbg cuttlefish2)
rlorigro commented 1 year ago

In addition to this, I've noticed that the headers are not installed to the installation destination, which is usually convenient for linking. Would it be possible to add a step to your CMake along these lines:

install(FILES ${headers} DESTINATION include/cuttlefish)
rob-p commented 1 year ago

Thanks @rlorigro — I'll tag @jamshed here but I'm sure he'll see it anyway. In the first case, I think we haven't tested including cuttlefish with FetchContent, but I think the fix should hopefully not be difficult.

In the second case, we don't install headers by default since (at least until this point) the primary user usecase has been to use cuttlefish as a standalone program rather than to interact with it programmatically as a library (apart from our own use in pisecm). However, it should be straightforward to also install the relevant files necessary to use cuttlefish as a library.

rlorigro commented 1 year ago

Nice, thanks for the quick reply. I've managed to fix the patch step error with this (admittedly hacky) solution:

add_custom_command(
        TARGET BUILD_CUTTLEFISH POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${CUTTLEFISH_DIR}/patches/kmc_patch.diff
        ${CMAKE_SOURCE_DIR}/patches/kmc_patch.diff)

It just copies the diff file into the place where it was looking for it

But now I'm getting a new error relating to finding the KMC headers. Haven't been able to fix it by including the directories manually

[ 55%] Building CXX object _deps/project_cuttlefish-build/src/CMakeFiles/cfcore_static.dir/kmc_api/kmc_file.cpp.o
/home/ryan/code/test_dbg/build/_deps/project_cuttlefish-src/src/kmc_api/kmc_file.cpp:12:10: fatal error: kmc_api/stdafx.h: No such file or directory
   12 | #include "kmc_api/stdafx.h"
      |          ^~~~~~~~~~~~~~~~~~

I suspect a lot of this is a result of my brute force method for linking, so If you have any recommendations for how to make this simpler, please let me know :)