How to get QMake to copy large data files only if they are updated - qt-creator

I have some large data files that need to be copied from source folders to build folders during our Qmake/QtCreator build. Since they are large, I only want the copy to happen for new/changed files. And I'd really like to avoid listing them all specifically in the project file. Here's what I've tried:
This attempt at copying data files fails because the DemoData folder is the target. Therefore the copy is not performed if files within the folder are added or changed. Only if the folder does not exist.
DemoData.commands = $$COPY_CMD $${SRC_DATA_DIR}DemoData $${BLD_DATA_DIR}DemoData
DemoData.target += $${BLD_DATA_DIR}DemoData
PRE_TARGETDEPS += $${BLD_DATA_DIR}DemoData
QMAKE_EXTRA_TARGETS += DemoData
This approach fails because the DemoData.target item is not expected to have a list of multiple items. QMake puts the list in quotes in the generated makefile so it becomes one target.
DemoData.commands = $$COPY_CMD $${SRC_DATA_DIR}DemoData $${BLD_DATA_DIR}DemoData
DEMO_DATA_FILES = $$files($${SRC_DATA_DIR}DemoData/*)
for(FILE, DEMO_DATA_FILES){
DemoData.target += $${BLD_DATA_DIR}DemoData\\$$basename(FILE)
PRE_TARGETDEPS += $${BLD_DATA_DIR}DemoData\\$$basename(FILE)
}
QMAKE_EXTRA_TARGETS += DemoData
This attempt fails because (AFAICT) QMake does not support variable names contained in other variables. It seems to be more of a one level substitution. A makefile is generated, but the DemoDataX targets all have no command lines. All attempts to display the contents of the 'commands' field generate syntax errors.
DEMO_DATA_FILES = $$files($${SRC_DATA_DIR}DemoData/*)
DEMO_DATA_NAME = DemoData
for(FILE, DEMO_DATA_FILES){
$${DEMO_DATA_NAME}.target = $${FILE}
$${DEMO_DATA_NAME}.commands = $$COPY_CMD $${FILE} $${BLD_DATA_DIR}DemoData
PRE_TARGETDEPS += $${FILE}
QMAKE_EXTRA_TARGETS += $${DEMO_DATA_NAME}
DEMO_DATA_NAME = $${DEMO_DATA_NAME}X
}
This approach works, but with two shortcomings. The minor one is that a separate 'make install' step must be performed. The major one is that the files are always copied unconditionally. Since our data files are large, this is unacceptable timewise.
DemoData.path = $${BLD_DATA_DIR}DemoData
DemoData.files = $${SRC_DATA_DIR}DemoData/*
INSTALLS += DemoData
Is there a way to do this, or am I left with some sort of external script or manually generated/maintained makefile?

Use QMAKE_EXTRA_COMPILES feature.
# list your files in this variable.
# Masks are available with $$files functions but
# if your set of files changes (files added or removed)
# your have to re-run qmake after that explicitly, not just make
MYFILES = $$files($${PWD}/files/*.*)
copy_files.name = copy large files
copy_files.input = MYFILES
# change datafiles to a directory you want to put the files to
copy_files.output = $${OUT_PWD}/datafiles/${QMAKE_FILE_BASE}${QMAKE_FILE_EXT}
copy_files.commands = ${COPY_FILE} ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
copy_files.CONFIG += no_link target_predeps
QMAKE_EXTRA_COMPILERS += copy_files
Add your big files to MYFILES variable. For each file a rule will be generated in Makefile that copies file to specified directory (datafiles in the example). Original file will be listed as a dependecy in the rule (this is default qmake behaviour) so copy will occur only when original file is fresher than existing copy. Generated rules are listed as dependencies in the target file rule (copy_files.CONFIG += target_predeps) so copying will occur on every build automatically.
The only caveat is this: if your set of files is dynamic (files are added or removed) you can use masks as in my example but you have to be careful to execute qmake after changing the set. Be aware that Qt Creator builds projects by launching make, not qmake. The most simple way to ensure that qmake will be launched is to modify .pro file.
For those who can read Russian there is more info about QMAKE_EXTRA_COMPILERS here

Do you need the script to be cross platform? I personally wouldn't use the copy command, but robocopy on Windows and rsync on Mac/Linux.
win32: $${DEMO_DATA_NAME}.commands = robocopy $${SRC_DIR} $${DST_DIR} $${FILE} /MIR /XO
!win32: $${DEMO_DATA_NAME}.commands = rsync -aru $${FILE} $${BLD_DATA_DIR}
I'm not really sure what you want to copy here, but you get the idea, you can adapt the files and/or directories.
Robocopy parameters are described here.
/MIR Mirrors a directory tree
/XO Excludes older files.
Rsync parameters are described here.
-a Archive
-r Recursive
-u Update only when the source is newer
As a side note if you don't want to run this make install command, you can set this extra target as a dependency to the project that needs these files: theProjectNeedingThoseFiles.depends += DemoData.

Related

Include path in makefile to include subdirectories

I have a directory which internal contains sub directories. I want to include the parent directory as include path in make file for header files. Is there a syntax so that all the sub directories are searched for the header files.
[EDIT] Posting the question in more detail
There are three sub folders in a parent folder
parentFolder/child1
parentFolder/child2
parentFolder/child3
There are header files in each all subfolders
Using -I option in makefile for header file path, i have to use
HEADER_PATH += -I./parentFolder
HEADER_PATH += -I./parentFolder/child1
HEADER_PATH += -I./parentFolder/child2
HEADER_PATH += -I./parentFolder/child3
Is there any way I can mention only the parent folder , but the search for header files will happen in all the subfolders also
http://www.gnu.org/software/make/manual/html_node/Recursion.html
Setup variable in first makefile and export it for sub-dirs. If you want to be able to invoke make in subdirs manually - i suppose best way to achieve this is either using configure-like systems to generate paths for you, or setting global variable (e.g. YOURPROJECT_DIR) in .profile or .bashrc and using it in makefiles.
I would like to see better solutions since i've encountered quite the same problem some time ago.

Using CMake, how can I concat files and install them

I'm new to CMake and I have a problem that I can not figure out a solution to. I'm using CMake to compile a project with a bunch of optional sub-dirs and it builds shared library files as expected. That part seems to be working fine. Each of these sub-dirs contains a sql file. I need to concat all the selected sql files to one sql header file and install the result. So one file like:
sql_header.sql
sub_dir_A.sql
sub_dir_C.sql
sub_dir_D.sql
If I did this directly in a make file I might do something like the following only smarter to deal with only the selected sub-dirs:
cat sql_header.sql > "${INSTALL_PATH}/somefile.sql"
cat sub_dir_A.sql >> "${INSTALL_PATH}/somefile.sql"
cat sub_dir_C.sql >> "${INSTALL_PATH}/somefile.sql"
cat sub_dir_D.sql >> "${INSTALL_PATH}/somefile.sql"
I have sort of figured out pieces of this, like I can use:
LIST(APPEND PACKAGE_SQL_FILES "some_file.sql")
which I assume I can place in each of the sub-dirs CMakeLists.txt files to collect the file names. And I can create a macro like:
CAT(IN "${PACKAGE_SQL_FILES}" OUT "${INSTALL_PATH}/somefile.sql")
But I am lost between when the CMake initially runs and when it runs from the make install. Maybe there is a better way to do this. I need this to work on both Windows and Linux.
I would be happy with some hints to point me in the right direction.
You can create the concatenated file mainly using CMake's file and function commands.
First, create a cat function:
function(cat IN_FILE OUT_FILE)
file(READ ${IN_FILE} CONTENTS)
file(APPEND ${OUT_FILE} "${CONTENTS}")
endfunction()
Assuming you have the list of input files in the variable PACKAGE_SQL_FILES, you can use the function like this:
# Prepare a temporary file to "cat" to:
file(WRITE somefile.sql.in "")
# Call the "cat" function for each input file
foreach(PACKAGE_SQL_FILE ${PACKAGE_SQL_FILES})
cat(${PACKAGE_SQL_FILE} somefile.sql.in)
endforeach()
# Copy the temporary file to the final location
configure_file(somefile.sql.in somefile.sql COPYONLY)
The reason for writing to a temporary is so the real target file only gets updated if its content has changed. See this answer for why this is a good thing.
You should note that if you're including the subdirectories via the add_subdirectory command, the subdirs all have their own scope as far as CMake variables are concerned. In the subdirs, using list will only affect variables in the scope of that subdir.
If you want to create a list available in the parent scope, you'll need to use set(... PARENT_SCOPE), e.g.
set(PACKAGE_SQL_FILES
${PACKAGE_SQL_FILES}
${CMAKE_CURRENT_SOURCE_DIR}/some_file.sql
PARENT_SCOPE)
All this so far has simply created the concatenated file in the root of your build tree. To install it, you probably want to use the install(FILES ...) command:
install(FILES ${CMAKE_BINARY_DIR}/somefile.sql
DESTINATION ${INSTALL_PATH})
So, whenever CMake runs (either because you manually invoke it or because it detects changes when you do "make"), it will update the concatenated file in the build tree. Only once you run "make install" will the file finally be copied from the build root to the install location.
As of CMake 3.18, the CMake command line tool can concatenate files using cat. So, assuming a variable PACKAGE_SQL_FILES containing the list of files, you can run the cat command using execute_process:
# Concatenate the sql files into a variable 'FINAL_FILE'.
execute_process(COMMAND ${CMAKE_COMMAND} -E cat ${PACKAGE_SQL_FILES}
OUTPUT_VARIABLE FINAL_FILE
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
# Write out the concatenated contents to 'final.sql.in'.
file(WRITE final.sql.in ${FINAL_FILE})
The rest of the solution is similar to Fraser's response. You can use configure_file so the resultant file is only updated when necessary.
configure_file(final.sql.in final.sql COPYONLY)
You can still use install in the same way to install the file:
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/final.sql
DESTINATION ${INSTALL_PATH})

Intltool with an autoconf-generated .desktop file

In the Emperor project, I'm having some issues getting intltool to work when doing an out-of-tree build. When running make check out-of-tree, which is one of the things make distcheck does, intltool fails thus:
INTLTOOL_EXTRACT="/usr/bin/intltool-extract" XGETTEXT="/usr/bin/xgettext" srcdir=../../po /usr/bin/intltool-update --gettext-package emperor --pot
can't open ../../po/../data/emperor.desktop.in: No such file or directory at /usr/bin/intltool-extract line 212.
intltool is looking for emperor.desktop.in, which is listed in po/POTFILES.in, in the source tree. However, emperor.desktop.in is generated by the configure script from a file called emperor.desktop.in.in, in order to insert the installed executable path as configured by the user, and lands in the build tree.
These are the relevant bootstrap.sh lines:
echo +++ Running intltoolize ... &&
intltoolize --force --copy &&
cat >>po/Makefile.in.in <<EOF
../data/_column_names.h:
cd ../data && \$(MAKE) _column_names.h
EOF
The setup code in configure.ac:
IT_PROG_INTLTOOL([0.35.0])
GETTEXT_PACKAGE=emperor
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"],
[The domain to use with gettext])
AM_GLIB_GNU_GETTEXT
data/emperor.desktop.in is listed in AC_CONFIG_FILES.
data/Makefile.am contains these lines:
desktopdir = $(datadir)/applications
desktop_in_files = emperor.desktop.in
desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
#INTLTOOL_DESKTOP_RULE#
and po/POTFILES.in contains the line
data/emperor.desktop.in
You can review all the details in the public git repository if you wish.
Can I somehow tell intltool that this file will be located in the build tree, not in the source tree? Otherwise, my options appear to be to break make distcheck (not a great option), or to ship a desktop file that doesn't include the full path and assumes that the executable is installed in the PATH. (just as messy, IMHO) - Any other options?
In your source code you have emperor.desktop.in.in, which does not seem to be in any rule as a dependency. That file has to be converted first to emperor.desktop.in and later to emperor.desktop, which does not seem to be the case in your data/Makefile.am.
desktopdir = $(datadir)/applications
desktop_in_in_files = emperor.desktop.in.in
desktop_in_files = $(desktop_in_in_files:.desktop.in.in=.desktop.in)
desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
#INTLTOOL_DESKTOP_RULE#
[...]
EXTRA_DIST = \
$(desktop_in_in_files) \
[...]
$(desktop_in_in_files) contains $(desktop_in_in_files), and Makefile will know how to deal with that.
Some further digging has brought me believe that the answer is: intltool does not support source files that aren't source files in the project. Ergo, any additional processing must be done after intltool is through
Intltool requires the lines in POTFILES to be relative to the (build-time) working directory. The file POTFILES is generated by the configure script from POTFILES.in with a simple sed script defined in the IT_PO_SUBDIR autoconf macro (called by IT_PROG_INTLTOOL) that simply prepends the relative location of the top-level source directory to the paths. Alas, modifying POTFILES does not help: the intltool-extract script does everything it can to get the source directory right. I don't believe files that are sometimes inside and sometimes outside the source tree can be supported without modifying intltool itself.

How do you a configure a Visual Studio custom build tool to depend on a lot of files?

I need to make sure a custom build tool that operates on a lot of files is always run when one of those files are changed.
I know you can specify "Additional Dependencies" for the custom build tool, but is there a better way than specifying one file per line?
Just made the build tool depend on one file. Created another custom build tool that runs before it and that touches this file if any of the real dependencies have changed. The advantage is that I now have quite a lot of flexibility and don't need to change any of the project settings if the dependencies change - that's taken care of via a database that the new custom build tool accesses.
"Additional Dependencies" is the correct and only documented way. You could adjust the contents of this field for the build tool in the Project file using an external tool to save on the troubles of doing a copy & paste & typo-adjust.
It won't be quite as reliable, but you could put all files in a subfolder and just make the folder a dependency.
A little bit hack:
(1) group the dependency files into a separate folder
(2) create a jscript file detect.js as follow:
var output = WScript.arguments(0);
var folder = WScript.arguments(1);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var objOutput = fso.GetFile(output);
var objFolder = fso.GetFolder(folder);
// if the output file is older than input folder,
// delete output file to force regenerate
if (objOutput.DateLastModified < objFolder.DateLastModified) {
fso.DeleteFile(objOutput);
} else {
// if the output file is older than one of files in the input folder,
// delete output file to force regenerate
var e = new Enumerator(objFolder.Files);
for (; !e.atEnd(); e.moveNext()) {
if (objOutput.DateLastModified < e.item().DateLastModified)
fso.DeleteFile(objOutput);
break;
}
}
}
(2) Add command lines to Pre-Build Event as follow:
cscript.exe /nologo detect.js $(Output) $(InputFolder)
(3) Setup the Custom Buld Step to force the Pre-Build Event to occur, i.e.
Command Line: echo --------------
Outputs: echo.fake
Execute After: PreBuildEvent

How can I set up an Xcode build rule with a variable output file list?

Build Rules are documented in the Xcode Build System Guide
They are well adapted to the common case where one input file is transformed into a fixed number (usually one) of output files.
The output files must be described in the "Output Files" area of the build rule definition; one line per output file. Typically the output files have the same name as the input file but have different extensions.
In my case, one single input file is transformed into a variable number of files with the same extensions. The number and the names of the output files depend on the content of the input file and are not known in advance.
The output files will have to be further processed later on (they are in this case C files to be compiled).
How can I set up a build rule for such a case?
Any suggestions welcome.
(I asked the same question on the Apple developer forum, but I figured it'd be a good idea to ask here too).
I dealt with this by, instead of generating multiple C files, just concatenating them all together into one file (e.g. "AUTOGENERATED.c"), and specifying that as the output file.
So long as your output files don't contain anything that will conflict (static functions with the same name, conflicting #defines etc.) this works well.
See this article on Cocoa With Love:
http://cocoawithlove.com/2010/02/custom-build-rules-generated-tables-and.html
This has an example of generating custom C code and using that as input to the normal build process. He's using ${} variable syntax in the output
The best way I found to add any number of files to my xcode project (and make some processing) is to write a little php script. The script can simply copy files into the bundle. The tricky part is the integration with xcode. It took me some time to find a clean way. (You can use the script language you like with this method).
First, use "Add Run Script" instead of "Add Copy File"
Shell parameter:
/bin/sh
Command parameter:
${SRCROOT}/your_script.php -s ${SRCROOT} -o ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}
exit $?
(screenshot in xcode)
${SRCROOT} is your project directory.
${CONFIGURATION(...) is the bundle directory. Exactly what you need :)
This way, your script return code can stop xcode build (use die(0) for success and die(1) for failures) and the output of script will be visible in xcode's build log.
Your script will look like that: (don't forget chmod +x on it)
#!/usr/bin/php
<?php
error_reporting(E_ALL);
$options = getopt("s:o:");
$src_dir = $options["s"]."/";
$output_dir = $options["o"]."/";
// process_files (...)
die(0);
?>
BONUS: here my 'add_file' function.
Note the special treatment for PNG (use apple's png compression)
Note the filemtime/touch usage to prevent copy files each times.
l
define("COPY_PNG", "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/copypng -compress");
function add_file_to_bundle($output_dir, $filepath) {
// split path
$path_info = pathinfo($filepath);
$output_filepath = $output_dir.$path_info['basename'];
// get file's dates of input and output
$input_date = filemtime($filepath);
$output_date = #filemtime($output_filepath);
if ($input_date === FALSE) { echo "can't get input file's modification date"; die(1); }
// skip unchanged files
if ($output_date === $input_date) {
//message("skip ".$path_info['basename']);
return 0;
}
// special copy for png with apple's png compression tool
if (strcasecmp($path_info['extension'], "png") == 0) {
//message($path_info['basename']." is a png");
passthru(COPY_PNG." ".escapeshellarg($filepath)." ".escapeshellarg($output_filepath), $return_var);
if ($return_var != 0) die($return_var);
}
// classic copy
else {
//message("copy ".$path_info['basename']);
passthru("cp ".escapeshellarg($filepath)." ".escapeshellarg($output_filepath), $return_var);
if ($return_var != 0) die($return_var);
}
// important: set output file date with input file date
touch($output_filepath, $input_date, $input_date);
return 1;
}

Resources