How do you perform an action over each file using make? - makefile

Let's say I have this:
FILES = c:/file.c c:/another_file.c
and I want to do something to each of the files. For example, I'd like to apply cygpath to each of the files. How can I do that? I would like a solution based on an external program, instead of a built-in make function please.

Your question is unclear. If you mean you want to get at the cygpath'd path names, use something like:
CYG_FILES := $(shell cygpath $(FILES))

Related

Using paths in different packages convenient way

I have a program in which I use a lot "../" which is to go one level up
in the file system and run some process on the directory with specific name. I have a command line tool in Go.
I have 3 questions
there is nicer way to do it instead of “../“
is there a const with which I can use instead of “/“
if 2 is not available should I create “constants“ under that internal package to share the “/“ between packages since I need it in
many place (from diff packages...)
example
dir.zip("../"+tmpDirName, "../"+m.Id+".zip", "../"+tmpDirName)
Set a variable, and use that everywhere:
path := "../"
or
path := ".." + string(os.PathSeparator)
then later:
dir.zip(path+tmpDirName, path+m.Id+".zip", path+tmpDirName)
This makes it very easy to change the path in the future, via a command line option, configuration, or just editing the value.
Yes. os.PathSeparator is the OS-specific path separator for the current architecture.
n/a
declare a global const somewhere, but I would just use ".." everywhere
os.PathSeparator
use filepath.Join("..", someDir, someFilename)

How to input a parameter in a custom target with cmake

I have a custom target:
add_custom_target(
create-po
COMMAND ${MSGINIT} --no-translator -i "${PROJECT_SOURCE_DIR}/data/${PACKAGE}.pot" - "${PROJECT_SOURCE_DIR}/po/es.po" -l es_MX.utf8
)
so, is invoked like this:
# make create-po
my idea is to change it to something like this:
# make create-po "es"
so, any user can create a custom localed po file. I don't know the word exactly for this, but I'd like to add a parameter in the target name..is it posible with cmake? Thanks
After so long time I found this question for the same reason: Can I use CMake to initialize a .po file if I want to add a new translation? I expect to use it only once in a while for my project, so make the build system do it seems more comfortable to me than find out all the required options and paths every time.
I ended up with the following CMake snippet:
set(INIT_LANG CACHE STRING "give a locale here to create a target which initializes a related .po file")
IF(INIT_LANG)
add_custom_target(
create-po-${INIT_LANG}
... # integrate INIT_LANG in your command
)
ENDIF(INIT_LANG)
Then, if you want to initialize a new translation file, call (assuming your build dir in under the project root):
# cmake -DINIT_LANG=es_MX.utf8 ..
... and you should get a corresponding make target:
# make create-po-es_MX.utf8
Yes, it's not as straight-forward as the OP's idea/expectation (and mine as well), but users can create new .po files by themselves (of course, this will be documented properly for them in the project ;) ).

How do I get the directory where the executable is located?

I got the filename like this:
_TCHAR filename[_MAX_PATH];
GetModuleFileName(NULL,filename,sizeof(filename));
How do I remove the filename from this full path? Should I use regex?
You can use the Windows shell API function PathRemoveFileSpec to do this. Example usage is listed on the linked page.
Since you use VS++, you can use:
_splitpath and _wsplitpath functions to break apart path

Makefile: ignore prerequisite if does not exist

Is there any way to say that if prerequisite for the given target doesn't exist then ignore that target?
For instance, I have the following set of folders
chrome_src_folders := $(chrome_src_folder)/content/* \
$(chrome_src_folder)/locale/* $(chrome_src_folder)/skin/*
This is where I use it
$(jar_path): $(chrome_src_folders)
zip -urq $(jar_path) $(chrome_src_folders)
Basically skin or locale may very well not be there, which will give me a nice error.
How to avoid that error and make the chrome_src_folders mandatory? or should I filter somehow chrome_src_folders and leave only those which exist?
There's more than one way to do this; the simplest is to filter the list using wildcard
chrome_src_folders := $(wildcard $(chrome_src_folder)/content/* \
$(chrome_src_folder)/locale/* $(chrome_src_folder)/skin/*)
Two thoughts; Since the skin and locale folders are optional, do you need to call them about as dependencies? Let the build commands take care of them if they need to. So something like:
chrome_content_folder := $(chrome_src_folder)/content/*
chrome_content_optional := $(chrome_src_folder)/locale/* $(chrome_src_folder)/skin/*
$(jar_path): $(chrome_content_folder)
zip -urq $(jar_path) $(chrome_content_folder) $(chrome_content_optional)
If you have to have the right folders on the dependency line, so you catch errors, I would write some macros that define when and how you require them. Then update your targets accordingly to only require those directories when you know they are required.

Concatenation of foldername and filenames in nmake

I have a Makefile for Nmake containing a list of files in a macro:
MYSRCFILES1=myfolder\file1.svg myfolder\file2.svg ... myfolder\file99.svg
and a second one just like this:
MYSRCFILES2=myfolder2\file1.svg myfolder2\file2.svg ... myfolder2\file99.svg
What I am trying is to avoid duplication of the list of files, and to avoid duplication of the folder names, something like this:
MYSRCFILES0=file1.svg file2.svg file3.svg
MYSRCFILES1="prepend 'myfolder\' to each element of $(MYSRCFILES0)"
MYSRCFILES2="prepend 'myfolder2\' to each element of $(MYSRCFILES0)"
Digging myself through the documentation of Nmake I haven't found a solution so far. Any idea how to accomplish this?
Finally found a solution for my problem, it's not perfect since I have to add a .\ to every file but that seems to be ok in my case:
MYSRCFILES0=.\file1.svg .\file2.svg .\file3.svg ...
MYSRCFILES1=$(MYSRCFILES0:.\=myfolder\)
MYSRCFILES2=$(MYSRCFILES0:.\=myfolder2\)
does the trick.
Is Nmake simillar to make? You can use patsubst ("pattern substitute string") function like this:
MYSRCFILES0=.\file1.svg .\file2.svg .\file3.svg ...
MYSRCFILES1=$(patsubst %,myFolder/%,MYSRCFILES0)
MYSRCFILES2=$(patsubst %,myFolder2/%,MYSRCFILES0)

Resources