So I wanted to create a make run command so that I can test locally. It loads some env vars before running it.
I have a win.env file
set ENV1=val1
set ENV2=val2
and a lin.env file
export ENV1=val1
export ENV2=val2
in the Makefile I have:
run:
ifeq ($(OS),Windows_NT)
. ./win.env && go run .
else
. ./lin.env && go run .
endif
On linux it works, but on windows it says:
'.' is not recognized as an internal or external command,
operable probram or batch file.
make: *** [run] Error 1
How can I load the env vars before running my program?
The . command is a feature of the POSIX shell. You are not using a POSIX shell on Windows, you're using Windows cmd.exe.
First, you can't name the file win.env. Windows cares a lot about file extensions so you'll have to name this file win.bat or something like that.
Second, in Windows cmd.exe you just run the script; it doesn't start a new command program like POSIX systems do.
run:
ifeq ($(OS),Windows_NT)
win.bat; go run .
else
. ./lin.env && go run .
endif
Related
I need the makefile generated by TI Code Composer Studio 10 to use SHELL = sh.exe instead of SHELL = cmd.exe. The insertion of the SHELL line seems to be specific to TI and not Eclipse. I can't figure out how to do this. I've tried setting environment variables at the system level and in the project build settings. I've tried running TI CCS from a bash shell hoping it might pick up its own environment. I've also looked through the Eclipse sources for generating the makefile.
Example:
Everything from SHELL to just before the -include line is TI specific.
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
SHELL = cmd.exe
CG_TOOL_ROOT := C:/ti/ccs1040/ccs/tools/compiler/ti-cgt-msp430_20.2.5.LTS
GEN_OPTS__FLAG :=
GEN_CMDS__FLAG :=
ORDERED_OBJS += \
"./msp430fr60x7_1.obj" \
"../lnk_msp430fr6047.cmd" \
$(GEN_CMDS__FLAG) \
-llibmpu_init.a \
-llibmath.a \
-llibc.a \
-include ../makefile.init
Reason: gmake creates batch files for commands like echo and our computers have security software that block randomly created batch files from running. If run from bash/sh, gmake doesn't use intermediate batch files.
Update: I may have a workaround by telling the project to invoke gmake like this: sh.exe -c gmake.exe args. That isn't working fully though as the args do not get passed.
After confirming with TI that the SHELL is hardcoded, this workaround is the solution.
You'll need to:
Create a make.sh shell script to run the real gmake.exe
Create a makefile.defs to fix some SHELL and RM variables inside the TI generated makefile
Install another sh.exe. I used the one installed with Git for Windows. The cygwin sh.exe that comes with the TI compiler doesn't work. This is a shame, because it would be nice to have a solution not requiring another tool.
First, modify your project build settings to run the make script. In TI Code Composer Studio:
Project Menu -> Show Build Settings -> CCS Build -> Builder
[ ] Uncheck "Use default build command"
Build Command: C:/Progra~1/Git/bin/sh.exe ${PROJECT_ROOT}/make.sh ${CCS_UTILS_DIR}/bin/gmake -k -j 8
Note: You have to use the MS-DOS short path for "Program Files," because it's 2021, and Eclipse can't handle spaces in a path.
Create make.sh in the PROJECT_ROOT directory:
#!C:/Progra~1/Git/bin/sh.exe
# The cygwin provided shell from TI doesn't work with make. Use the one from Git instead or even SourceTree's embedded Git shell.
# In TI CCS, Project -> Show Build Settings -> CCS Build -> Builder -> Build Command (Uncheck Use default build command)
# Build Command: C:/Progra~1/Git/bin/sh.exe ${PROJECT_ROOT}/make.sh ${CCS_UTILS_DIR}/bin/gmake -k -j 8
echo "$#"
# debugging
#echo $#
#env | sort
if [ $# -eq 0 ]; then
echo Nothing to do. >&2
exit
fi
export MAKESHELL="$SHELL"
# echo MAKESHELL is \'$MAKESHELL\' inside the script, but will be translated to the DOS path inside make.
"$#"
Create makefile.defs in the PROJECT_ROOT directory. The Eclipse generated makefile includes this file at the very end allowing it to override any settings set by TI.
# The invoking shell sets MAKESHELL so we can grab it here.
SHELL = $(MAKESHELL)
RM = rm -f
$(info "MAKEFLAGS is $(MAKEFLAGS)")
$(info "MAKECMDGOALS is $(MAKECMDGOALS)")
# $(info "SHELL is $(SHELL)")
# $(info "CWD is $(CWD)")
# $(info "PATH is ${PATH}")
I have a very simple makefile that I'm trying to execute in git-bash on a Windows 10 machine.
The contents of the makefile are:
start:
source env.sh
Where env.sh is a local file that exists.
If I execute the command make start I receive the following error:
process_begin: CreateProcess(NULL, source env.sh, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [Makefile:2: start] Error 2
However, if I execute source env.sh at the command prompt all is well and I receive no error at all.
I've seen other posts like this Windows 10 Makefile error make (e=2): The system cannot find the file specified which report a similar error, but the linked question is to do with docker being on the path which I don't think applies here.
Is there any reason why the makefile errors but typing the command does not?
On Windows by default, GNU make always runs the Windows cmd.exe shell. The recipe that you're running here is a bash command.
You say it works at the command prompt but that's because the command prompt you're using is from Git bash; if you opened a Windows terminal and typed that command it wouldn't work.
If you want to use a different shell than the default you need to set the SHELL make variable in your makefile:
SHELL := <path to git bash>
I'm seeing a strange issue when trying to change directory from inside a Makefile in Windows. My pseudo-code is as follows:-
all:
cd ../ProjectDir && ../AutoExc InputFile.h
Where AutoExc is an executable that I have in the parent directory. When I run 'make' from the command line, I see the following output:-
cd ../ProjectDir && ../AutoExc InputFile.h
'..' is not recognized as an internal or external command.
The strange thing is that running this exact Makefile in Linux works. I have also tried running the above command from the command line in Windows and it worked, so there isn't an issue with 'cd' for example.
Any idea why this is happening and what can I do to get it to work? If there's an issue with the version of Make, is there another more reliable mechanism to change directory and run an executable on Windows?
I am using Make version 3.81 on Windows 7 (latest) and Make version 4.1 on Linux 4.15.0-20-generic #21-Ubuntu. I have 'cd' installed as part of 'Git for Windows', the git version is 2.16.1.windows.4.
Your command contains shell characters and is passed down to cmd.exe:
>cd ../Public && ../test
'..' is not recognized as an internal or external command,
operable program or batch file.
>cd ../Public && ..\test
'..\test' is not recognized as an internal or external command,
operable program or batch file.
i.e. cmd.exe interprets ../test as command .. with option /test.
I guess one solution would to apply a macro to command names with paths, e.g. (note: untested, just typed from my head):
if (...I'm running under Windows...)
convert_path = $(subst /,\\,$(1))
else
convert_path = $(1)
endif
all:
cd ../ProjectDir && $(call convert_path,../AutoExc) InputFile.h
or, if possible, use the suggestion from the comments SHELL := /bin/sh, or whatever the correct path is to a UNIX compatible shell in your Windows environment. "Git for Windows" is AFAIR based on MinGW, so you should have bash available.
I had learned Makefile before, but there is a problem in Makefile when I typed the command "make" today.
Here is the piece of code:
[ -d _build ] || mkdir _build
And the error:
'[' is not recognized as an internal or external command, operable program or batch file.
and there is my Make version:
GNU Make 3.82.90
Built for i686-pc-mingw32
Can anybody tell me how to fix it?
Your makefile was written for Linux/Unix and you are running it under Windows.
In a makefile, the commands that compose the recipes of the targets are
written in the language of the OS shell. Under Windows the shell is cmd.
Under Linux by default it is bash. Under other Unix variants it is sh or
some other shell.
[ -d _build ] || mkdir _build
is a valid command for bash and some other Linux/Unix shells. Not for
cmd. The same will be true of almost all the other recipe commands in the
makefile. You have just hit the first such problem.
Your makefile is of no use under native Windows. On Windows you have a chance
of running it in a Linux virtual machine, or under the Windows 10 Subsystem for Linux,
or under a Unix-like environment for Windows such as Cygwin or MSYS2. You may
well need to install tools that the makefile recipes require in that Linux/Unix-like
system.
If the makefile is simple you can consider translating the unrecognized shell
commands into cmd commands, if you are able.
I'm trying to write a script shell (Mac OS X) for deploying my Awestruct site within a launch agent.
So, I have to generate site before deploying it. The command to generate site is :
awestruct -g
My script is myscript.sh :
cd /my/site/structure/base/directory
awestruct -g
This script is launched by the system when an event occurs.
But the problem is it doesn't know about awestruct...
My awestruct program is a part of my ruby installation and is added to my PATH variable.
When I run the script manually, it works (because it's my user who is lauynching it)
When the system detect the event and runs the script, it results as :
awestruct: command not found
The problem is the PATH...
If it works manually, then in the same prompt where it works, run command:
which awestruct
That will print the program file with full path, let's assume /usr/local/bin/awestruct, but use whatever it really is. Then use that in your script, for example:
cd /my/site/structure/base/directory
/usr/local/bin/awestruct -g
This assumes that there are no other environment variables, only defined for your account, which awestruct needs. If there are, then find out what they are, and add them to your script before running awestruct, for example with line:
export AWESTRUCT_ENVIRONMENT_VARIABLE=foobar
(Note: When you run the script normally like any program, that will not change the parent shell environment.)
You can also add the path to the executable in the PATH of the user that run the script and gets the error.
You could try something like :
$ su - <user_that_run_the_script>
$ echo "export PATH=$PATH:$(which awestruct)" >> ~/.bash_profile
$ source ~/.bash_profile
(For Linux users, use ~/.bashrc instead of ~/.bash_profile)