# Copying script files to bin, checking them, setting execute bit.

**URL:** https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577
**Category:** Code
**Created:** [June 16, 2021, 3:15pm UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577 "2021-06-16T15:15:02Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Thorbjorn\_Willoch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thorbjorn_willoch/32/1560_2.png) [@Thorbjorn\_Willoch](https://discourse.cmake.org/u/Thorbjorn_Willoch)
#### Post date: [June 16, 2021, 3:15pm UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/1 "2021-06-16T15:15:02Z")

</div>

I have a project that consist of C/C++/proto/bash.

The C/C++ now works fine. proto works via an add\_custom\_command.  
(The cpp\_generate\_proto did not work with my protofiles in subdirectories)

But I have not found a satisfying way of copying the script files to  
the bin directory.

I have seen from previous answers that configure\_file(… COPY\_ONLY)  
been suggested.

And it kind of works. But the copying of those files are at the  
generator phase. So when I change one of those files cmake are  
refgenerating build files. Also it will not detect if the files are removed.  
So rm bin/\*.sh is not detected, and I have to run cmake . again.  
And it does not change the execute bit.

I could change the execute bit in the repository, and it will kind of  
work, but I do not like it.

It also does not do syntax check on the scripts.

What I would like to happen, when building, and when a script in the  
source tree is changed, is:

bash -n $src && cp $src $trg && chmod +x $trg

I have tried with add\_custom\_command/add\_custom\_target/add\_dependencies  
with no luck.

What I have for now ended up with is:

add\_custom\_target(Scripts ALL  
make VPATH=${CMAKE\_SOURCE\_DIR} -f ${CMAKE\_SOURCE\_DIR}/MakeShellScripts  
)

And let the MakeShellScripts makefile do the work.

But is there really not possible to do this inside CMake ?  
Define a target, sources, and how to convert the sources to the target?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 16, 2021, 4:39pm UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/2 "2021-06-16T16:39:51Z")

</div>

I would recommend something like [this function](https://github.com/Kitware/sprokit/blob/6b3f6cf87f625904e9061a447bd16f96800fef77/conf/sprokit-macro-configure.cmake). You can add the permission changing logic in there as well.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 16, 2021, 4:40pm UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/3 "2021-06-16T16:40:39Z")

</div>

[This issue](https://gitlab.kitware.com/cmake/cmake/-/issues/18711) may also be of interest.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [June 17, 2021, 12:32am UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/4 "2021-06-17T00:32:22Z")

</div>

Quite sure that I did miss something of the real problem. Why is a simple custom\_command not working?

```auto
add_custom_command(
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/tool.sh"
  COMMAND bash -n "${CMAKE_CURRENT_SOURCE_DIR}/tool.sh"
  COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/tool.sh" "${CMAKE_CURRENT_BINARY_DIR}/tool.sh"
  COMMAND chmod +x "${CMAKE_CURRENT_BINARY_DIR}/tool.sh"
)
add_custom_target(tool_check_and_copy ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tool.sh)

```

---

<div class="post-metadata">

### Author: ![Thorbjorn\_Willoch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thorbjorn_willoch/32/1560_2.png) [@Thorbjorn\_Willoch](https://discourse.cmake.org/u/Thorbjorn_Willoch)
#### Post date: [June 18, 2021, 8:32am UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/5 "2021-06-18T08:32:23Z")

</div>

You did not test this, did you?  
But you were right. Just add a  
`DEPENDS tool.sh`  
at the end of add\_custom\_command and it works.

I had tried several variations of your suggestion, but used add\_dependencies to the custom\_target, which apparently was not the same as DEPENDS.

Thanks.

I have 39 of these targets, so next project is to write a function to generate this.

Still surprised that such a function is not part of standard cmake. Though that it was quite common to have bash scripts or python scripts as part of a C/C++ baseline.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [June 18, 2021, 11:06am UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/6 "2021-06-18T11:06:33Z")

</div>

Oh, yes. I forgot the `DEPENDS` in this example. I just tried out if it does the right thing if the output was deleted.

You should add all relevant dependencies to the `add_custom_command` itself. This can’t be done with `add_dependencies` as it is not a target.

---

<div class="post-metadata">

### Author: ![Thorbjorn\_Willoch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thorbjorn_willoch/32/1560_2.png) [@Thorbjorn\_Willoch](https://discourse.cmake.org/u/Thorbjorn_Willoch)
#### Post date: [June 19, 2021, 2:46pm UTC](https://discourse.cmake.org/t/copying-script-files-to-bin-checking-them-setting-execute-bit/3577/7 "2021-06-19T14:46:00Z")

</div>

I had used add\_dependencies to the add\_custom\_target. Which did not works.

My current implementation:

```auto
function(add_bash file)
  get_filename_component(filename ${file} NAME)
  get_filename_component(abspath ${file} ABSOLUTE)
  set(out ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${filename})
  add_custom_command(
    OUTPUT ${out}
    COMMAND bash -n ${abspath}
    COMMAND cp ${abspath} ${out}
    COMMAND chmod +x ${out}
    DEPENDS ${file}
    )
  set(ScriptList ${ScriptList} ${out} PARENT_SCOPE)
  message( ${abspath})
endfunction()

function(add_many_bash target)
  set(ScriptList)
  foreach(i IN LISTS ARGN)
    add_bash(${i})
  endforeach()

  add_custom_target(${target} ALL
    DEPENDS ${ScriptList}
    )
  
endfunction()

```
