Copy files before compilation - qt

I've a master project with many sobprojects, that I compile using qmake.
In a sub-project I must copy some files before compilation (some header file). I've seen some command to perform operation before and after linking, but I'd like to know if it's possible to perform some shell operation before start compilation. I can't refer to them, but I must to copy them (don't ask why please, it's not my fault :-( ). Any suggestion?
Thanks in advance for your replies.

see my last answer on nearly the same question:
Copy some file to the build directory after compiling project with Qt
the only difference for you is to change in point 5:
POST_TARGETDEPS += copyfiles ## copy files after source compilation
to:
PRE_TARGETDEPS += copyfiles ## copy files before source compilation
when executing qmake there have to exist all files already in filesystem before

I think what you want to do can be accomplished with careful use of the QMAKE_EXTRA_COMPILERS and QMAKE_EXTRA_TARGETS variables. The Qt Labs article The Power of QMake gives a reasonable introduction to it. The ".commands" part of the extra compiler can be any arbitrary command, including a shell command.
The other suggestion I found in this e-mail exchange is to "... take a look at mkspecs/features/*.prf - especially those of moc and uic.." for other possible ways to do it.

I also just played around with QMAKE_EXTRA_TARGETS to solve the question, but could not manage to get it done ;)
One other (simple) solution which might work for you is to wrap the call to gcc/g++: in the .pro file, set QMAKE_CXX=./g++Wrapper and in the g++Wrapper shell script, call the original compiler while doing anything you want before and after the call:
#!/bin/bash
DoWhateverYouWantBeforeCompilation
g++ $*
DoWhateverYouWantAfterCompilation
By evaluating the command line parameters, you could also restrict your actions to specific files.

Related

VIM: how to get the file path/directory of opened buffer and do something?

my scenario is: I'm using vim to open some .cpp files, for example
vim 1.cpp src/2.cpp root/src/3.cpp
Sometimes, I wish to rebuild 3.cpp so I have to use another window to
"rm root/src/3.o"
and inside vim, type
":make"
This works fine, NP. But I am looking for a .vimrc function/command that:
When I switch to buffer, e.g. "root/src/3.cpp" and press this command, vim will detect the directory of "root/src" and the file name without suffix "3", and automatically execute a command of "rm root/src/3.o".
In this case, I can casually switch to any buffer and re-trigger the build of this very file.
Note I don't wish to map gmake tool command like "make clean" because we use several different make utilities like scons, cmake, etc.
So how to write this function/command in .vimrc? Thanks.
:call system('rm '.expand('%:p:r')) as #Kent said, or even simply :!rm %:p:r.
But I'm quite surprised you need to do that. Tools in charge of compilation chains usually understand dependencies (which ever the tool is), and you shouldn't need to remove the object file that often to need a mapping to do it for you.
PS: it's perfectly possible (but I need to update the doc) to support CMake, or out-of-source compilation from vim. But indeed, with out-of-sources compilation, you wouldn't need to delete those files manually, a :make clean if :make already works.
you can get root/src/3 from root/src/3.cpp buffer by:
expand('%:p:r')
Then you are free to concatenate the .o to end, and build the command.

Qt: How to create a directory in project file?

I just want to crossplatform make a single directory from *.pro file. I use some commands like $(COPY_DIR) and $(COPY_FILE) well. And I saw in internets a many examples with command $(MKDIR) but it did not work for me.
It prints:
C:/Projects/installer/installer.pro(24): Extra characters after test expression.
when I used $$(MKDIR) on line 24.
Or:
C:/Projects/installer/installer.pro(24): '$' is not a recognized test function.
when I $(MKDIR).
What the proper way to create a directory in qmake projects?
Short answer
Use QMAKE_MKDIR like so:
mytarget.commands += $${QMAKE_MKDIR} $$shell_path($${OUT_PWD}/foo)
Long answer
qmake provides variables holding useful commands. Take a look at Undocumented QMake article on Qt Wiki. The one you are looking for is QMAKE_MKDIR, but you might also be interested in QMAKE_CHK_DIR_EXISTS.
If the values given by qmake do not suite you, you can use the environment to retrieve the mkdir command:
$(MKDIR) $$shell_path($${OUT_PWD}/foo)
$$(MKDIR) $$shell_path($${OUT_PWD}/foo)
The $(...) syntax retrieves the environment variable when make (or nmake...) is run while $$(...) retrieves it when qmake is run.
Also the mkdir command should be call in the context of a "make target" declared with QMAKE_EXTRA_TARGETS. See Adding custom targets in qmake documentation.

QMake message not printing

All, I am attempting to work with a monolithic build system given to me by someone else (isn't that always the case?) and wanted to add some debug statements for myself.
The main .pro file uses a subdir template with the concept of:
SUBDIRS += durp-dir
durp-dir.file = Durp/durp.pro
When I add a message("In Durp") to durp.pro it never gets printed. I can't figure out what I'm missing. I'm using QT 4.8 ... Still examining the qmake documentation with no leads.
Run qmake -r instead of just qmake

source_dirs doesn't work in .gpr scipt

I've inherited an Ada/C++ project and I'm trying to use gprbuild to automate the build process (which was previously done with a set of about 12 .bat files). I'm totally new to Ada and gprbuild, but have actually made pretty good progress. I can compile the .exe's that I need, but not the library. I am not at liberty to completely share the .gpr file, but the relevant parts look like this:
[snip]
for Source_Dirs use (
"c_plus_plus_files",
"ada_files",
"..\another_project\some_other_ada_files",
"..\another_project\even_more_ada_files"
);
[snip]
for Source_Files use (
"my_ada_file.ads",
"another_ada_file.ads",
"one_more_ada_file.adb",
"c_plus_plus_file.cpp"
);
[snip]
When I run "gprbuild -P my_project.gpr" it in turn runs "gcc -c gnat5 one_more_ada_file.adb" and complains that it cannot find a certain file that one_more_ada_file.adb depends on. The dependency is in ..\another_project\even_more_ada_files, so I would expect it to be found. But if I copy the dependency into the same folder as one_more_ada_file.adb, the error goes away.
Because of how the VCS is setup and how we're sharing code between two projects, I'd much rather figure out what's wrong with how I'm using "for source_dirs use" than to keep multiple copies of all the ada files.
Again, I'm an Ada/GPS newb, so if I'm leaving out relevant information, please let me know.
Update: It appears that the specific problem isn't that source_dirs isn't doing anything at all, but that it doesn't handle having two source dirs where .ads files in one dir depend on .ads files in the other. That is, even within my "other" project above, an .ads file in some_other_ada_files that depends on an .ads file in even_more_ada_files doesn't get compiled with the gcc -c -gnat05 command when I run gprbuild (error: the file in even_more_ada_files not found), but it does get compiled if I run the gcc command by hand (or in a .bat script) with two -I flags, one for each directory.
When dealing with multiple projects, you should normally create a .gpr-file for each project, and let your projects depend on the other projects as needed.
Thus:
project another_project is
for Source_Dirs use
("some_other_ada_files",
"even_more_ada_files");
end another_project;
and then:
with "..\another_project\another_project.gpr"
project The_Project is
for Source_Dirs use
("c_plus_plus_files",
"ada_files");
end The_Project;

How to execute shell command after compile finished from .pro in QT?

What changes must I make to the .pro file if I want to execute chmod command, execute the output binary file, or do some other operations.
I had a similar problem. I wanted a special tool (versioner) to run over the code every time the Makefile was executed. Here's the solution:
(to be read in the Qmake Manual, Configuring qmake's Environment, Section: Customizing Makefile Output)
Create you own Makefile target. Specify the command etc.
mytarget.target = .buildfile
mytarget.commands = touch $$mytarget.target
QMAKE_EXTRA_TARGETS += mytarget
This way, you have an extra target you can call with make mytarget for example. If you want to tie it together to the actual buildtarget you'll have to add:
POST_TARGETDEPS += mytarget
Hope that helps.
Best regards
D
Another way to make things in given order is to use empty "super" target:
super.depends = target_pre first target_post
QMAKE_EXTRA_TARGETS += super
Where first - is default qmake target, and target_pre and target_post some custom targets. Now make super just do the thing.
EDIT: looks like in last versions of Qt build of dependencies is running in paralell so this solution wouldn't work.
If you are using Qt Creator, you can add custom build steps in the Projects panel: http://doc.qt.nokia.com/qtcreator-2.1/creator-build-settings.html#adding-custom-build-steps
The right answer depends on exactly what you want, and when. However, as seen in some previously posted comments here QMAKE_POST_LINK is probably what you want rather than POST_TARGETDEPS.
Check out this related post:
QMake: execute script after build
For one, when you use POST_TARGETDEPS that fires off BEFORE your exe is created (in Windows) or BEFORE it is recreated (in Linux)! QMake works differently depending upon the platform and the complier.
I needed to do some "symbols processing" on an exe when it was recompiled. POST_TARGETDEPS gave me problems in both Windows (using mingw) and Linux (using gcc). In Windows, it executed my script prematurely, and in Linux it overwrote my exe after I had modified it (i.e. added back my debugging info to the exe after I had stripped it in my external script). QMAKE_POST_LINK worked perfectly, however, in both cases. It's also short, sweet, and more clear by comparison!

Resources