Search code examples
c++cbuildmeson-build

Meson copy/install header files to output directory and keep folder structure


Basically i would like to be able to have a mix between the functions install_subdir and install_headers.
I want to copy all header files from my project source directory over to some other directory and still maintain the subdirectory structure.

Source

MyProject  
|-- folder1  
|   |-- file11.h  
|   |-- file11.cpp  
|   |-- file12.h  
|   `-- file12.cpp  
`-- folder2  
    `-- file21.h

Destination


MyProject
|-- folder1
|   |-- file11.h
|   |-- file12.h
`-- folder2
    `-- file21.h

What I tried was copying the source directory and exclude all cpp files and to just use the intended install_headers() function but both didn't work out.
I added comments to things I did and why. Maybe somebody knows what's up:

project('MyProject', 'cpp')

test_src = [  'src/MyProject/folder1/file11.cpp'
              'src/MyProject/folder1/file12.cpp']

# Passing files seems to be preferred but exclude_files only takes list of strings
# test_src = files([  'src/MyProject/folder1/file11.cpp'
#                     'src/MyProject/folder1/file12.cpp'])

test_hdr = files([  'src/MyProject/folder1/file11.h',
                    'src/MyProject/folder1/file12.h',
                    'src/MyProject/folder2/file21.h'])


base_dir = meson.current_source_dir()

static_library('MyProject', name_prefix: '', name_suffix : 'lib', 
                sources: test_src,
                install: true,
                install_dir: base_dir + '/build/lib')

# Produces flat hierarchy       
install_headers(test_hdr, install_dir: base_dir + '/build/include')

# Also copies all cpp files into destination folder
install_subdir('src/MyProject', install_dir: base_dir + '/build/include', exclude_files: '*.cpp')

# Same result as wildcard exclusion
install_subdir('src/MyProject', install_dir: base_dir + '/build/include', exclude_files: test_src)

Does anyone have a solution to this?
I have a full list of my sources and headers if that is necessary for any method.
I am currently copying the files via shell commands, but including this into the install/build process would be preferred.

// EDIT:
I am using meson-build on windows.


Solution

  • I found a work-around that does what i want.
    If someone finds a better solution to this that requires a list of all header files feel free to still answer and i will accept it.

    For now here is what i did:

    project('MyProject', 'cpp')
    
    
    test_src = files([  'src/MyProject/folder1/file11.cpp',
                        'src/MyProject/folder1/file12.cpp' ])
    
    # Note that i'm omitting the base folder here since exclude_files searches relative to the subdir path
    # Also, forward slash doesnt work. You have to use double backslash
    test_src_exclude = [  'folder1\\file11.cpp',
                          'folder1\\file12.cpp' ]
    
    dir_base = meson.current_source_dir()
    dir_install = join_paths(dir_base, 'build/meson-out/MyProject')
    dir_install_hdr = join_paths(dir_install, 'include')
    
    static_library('MyProject', name_prefix: '', name_suffix : 'lib', 
                    sources: test_src,
                    install: true,
                    install_dir: dir_install)
    
    install_subdir( 'src/MyProject', 
                    install_dir: dir_install_hdr, 
                    strip_directory: true,
                    exclude_files: text_src_exclude)