0

I'm have an installation that has the form:

git clone ...
mkdir build
cd build
cmake ..
make

But I am unable to do this. I tried something like:

ExternalProject_Add(my_ext_project
    GIT_REPOSITORY repo...
    SOURCE_DIR "project_src"
    CONFIGURE_COMMAND  mkdir build
    BINARY_DIR "project_src/build"
    BUILD_COMMAND cmake .. COMMAND make
 )

But when I try build, I keep getting

/bin/sh: cd: .../build: No such file or directory

How can I make a directory with ExternalProject_Add?

user2602914
  • 301
  • 3
  • 11
  • Drop the double quotes aroung the `mkdir build`. See also that question: https://stackoverflow.com/questions/55711749/cmake-add-custom-command-fails-with-bin-sh1-not-found – Tsyvarev Apr 21 '19 at 09:24
  • dropping the quotes only gets me the error "cd: .../build: No such file or directory" edited the question to reflect this. – user2602914 Apr 21 '19 at 09:30
  • In your `ExternalProject_Add` invocation I don't see the COMMAND which performs `cd ../build`. Also, this command looks useless: CMake automatically calls `BUILD_COMMAND` from the binary directory. – Tsyvarev Apr 21 '19 at 09:33
  • It seems that the "mkdir build" is being called after cmake tries to cd into BINARY_DIR – user2602914 Apr 21 '19 at 09:45
  • Hm, I first time see that someone bothers about creating binary directory in ExternalProject - I would assume CMake itself to handle that. Try to move `BINARY_DIR` setting **above** the `CONFIGURE_COMMAND`. – Tsyvarev Apr 21 '19 at 10:19

2 Answers2

0

OK, finally figured is out. The "mkdir build" command should be put after the "PATCH_COMMAND" tag like so:

ExternalProject_Add(my_ext_project
    GIT_REPOSITORY repo...
    SOURCE_DIR "project_src"
    #CONFIGURE_COMMAND  mkdir build ### wrong place for it! ###
    PATCH_COMMAND  mkdir build
    BINARY_DIR "project_src/build"
    BUILD_COMMAND cmake .. COMMAND make
 )
user2602914
  • 301
  • 3
  • 11
0

It seems that ExternalProject_Add doesn't like a binary directory inside the source one. From the ExternalProject documentation:

Note: If a download method is specified, any existing contents of the source directory may be deleted. Only the URL download method checks whether this directory is either missing or empty before initiating the download, stopping with an error if it is not empty. All other download methods silently discard any previous contents of the source directory.

Fortunatly, for the most projects a binary directory actually does not need to be inside the source one: the projects' READMEs give <src>/build directory as just an example of build directory.

For binary directory outside of the source one, ExternalProject_Add creates binary directory automatically.

One just need to make sure that BINARY_DIR option comes before any option which depends on that directory setting:

ExternalProject_Add(my_ext_project
    GIT_REPOSITORY repo...
    SOURCE_DIR "project_src"
    # Setting BINARY_DIR should come **before** CONFIGURE_COMMAND and BUILD_COMMAND
    BINARY_DIR "project_bin"
    # It is a way to refer to source directory in the ExternalProject_Add COMMAND.
    CONFIGURE_COMMAND cmake <SOURCE_DIR>
    BUILD_COMMAND make
)

Note, that such CONFIGURE and BUILD commands are default for ExternalProject_Add, so you may omit them:

ExternalProject_Add(my_ext_project
    GIT_REPOSITORY repo...
    SOURCE_DIR "project_src"
    BINARY_DIR "project_bin"
    # "Standard" CMake project doesn't require to specify any COMMAND.
)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Thanks! But unfortunately this did not work for me. What was happening was that cmake would make the directory, but then after the git repo step the directory would disappear. I guess it was something to do with the cloning. – user2602914 Apr 22 '19 at 03:15
  • Yes, it seems to be a specific of ExternalProject_Add about build directory inside the source one. Actually, you are not enforces to do so: you may use any build directory which you want. I have updated the answer post. – Tsyvarev Apr 22 '19 at 08:41
  • Wow, thanks again! But I was able to make a directory within the source directory by specifying a patch command like so: "PATCH_COMMAND mkdir build". This seems to work since 1) patch command runs after the git cloning, 2) it happens in the source directory and not the binary directory, and 3) it happens before the configure commands. Would it be possible to add this to your answer so it'll be more complete? – user2602914 Apr 23 '19 at 03:08
  • Having different answers with different approaches fits perfectly for Stack Overflow. There is no needs to make a single answer which describes all approaches – Tsyvarev Apr 23 '19 at 09:09