How can I set makefile variables (CC, CFLAGS etc) to terminal or cmd using powershell scripts? - powershell

I want to write platform independent Makefile's environment variable setup script which can set Makefile's environment variable like CC, CFLAGS, LDFLAGS, LD etc. This is my make file. I want to run it on Window or Linux as per the user need. So instead of setting CC, CFLAGS, LDFLAGS, LD every time, I want to write a script which can set the variable for user depending on which platform they are using.
LDFLAG=-L..\..\test\lib
LIBS=-ltestlibs
INCLUDES = ..\..\test\inc
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
EXECUTABLE = targetImage.exe
.PHONY: clean
all: myprog
myprog: $(OBJS)
$(CC) $(OBJS) $(LDFLAG) $(LIBS) -o $(EXECUTABLE)
$(OBJS): $(SRCS) $(INCLUDES)
$(CC) $(CFLAGS) -I$(INCLUDES) -c $(SRCS)
clean:
$(RM) $(OBJS) $(EXECUTABLE)
I know the .sh script but it can be run on CYGWIN or MINGW.
#!/bin/sh
echo "Finding the current OS type"
echo
osType="$(uname -s)"
#osType=$1
case "${osType}" in
"CYGWIN")
{
echo "Running on CYGWIN."
CURRENT_OS=CYGWIN
export CC=""
} ;;
Linux*)
{
echo "Running on Linux."
CURRENT_OS=Linux
export CC="-g -Wall"
} ;;
"MINGW")
{
echo "Running on MINGW."
CURRENT_OS=MINGW
} ;;
"Thor96")
{
source /opt/fsl-imx-xwayland/4.14-sumo/environment-setup-aarch64-poky-linux
CURRENT_OS=THOR96
};;
*)
{
echo "Unsupported OS:${osType}, exiting"
exit
} ;;
esac
echo ${CURRENT_OS}
But it can run on linux only. So how can I achieve the same using powershell .ps1 scripts ? So same script can work on any platform.
I have drafted this .ps1 file for reference but need to update it as I am not sure it's correct or not. Please guide me with proper solution. I am not able to find the proper solution for this.
Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
$Env:CC = "gcc"
}
Function RunOn-Linux
{
Write-Host 'The Script is Running on a Linux Machine'
$Env:CC = "gcc"
}
Function RunOn-Mac
{
Write-Host 'The Script is Running on a Mac'
}
Function RunOn-Other
{
Write-Host 'The Script is Running on a Other'
$Env:CC = "aarch64-poky-linux-gcc"
}
If ($IsWindows)
{RunOn-Windows}
elseif ($IsLinux)
{RunOn-Linux}
elseif ($IsMacOS)
{RunOn-Mac}
else
{RunOn-Other}

I don't have a Windows box to do tests but if yours has GNU make available there is a chance that you can do what you want from inside your Makefile:
# List supported OS (as returned by uname -s, case sensitive)
SUPPORTED_OS := CYGWIN Linux MINGW Darwin
# Get OS
OS := $(shell uname -s)
# Check if OS is supported
ifneq ($(filter-out $(SUPPORTED_OS),$(OS)),)
$(error Unsupported OS: $(OS))
endif
# Define all OS-dependent make variables as OS_VARIABLE
# Linux:
Linux_CC := gcc
Linux_CFLAGS := -g -Wall
Linux_LDFLAGS := ...
# CIGWIN:
CYGWIN_CC := ...
CYGWIN_CFLAGS := ...
CYGWIN_LDFLAGS := ...
...
# Assign make variables
CC := $($(OS)_CC)
CFLAGS := $($(OS)_CFLAGS)
LDFLAGS := $($(OS)_LDFLAGS)

Related

Issue running OpenSSL on Windows

I get the following error when I try to run the below code with openssl dependencies.
Link to Code: https://github.com/TimonPost/udp-dtls
Error:
Compiling openssl-sys v0.9.72
error: failed to run custom build command for `openssl-sys v0.9.72`
Caused by:
process didn't exit successfully: `C:\Users\Administrator\Desktop\wintun-main\target\debug\build\openssl-sys-4454fbdd0966bbd8\build-script-main` (exit code: 101)
--- stdout
cargo:rustc-cfg=const_fn
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_LIB_DIR
X86_64_PC_WINDOWS_GNU_OPENSSL_LIB_DIR unset
cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
OPENSSL_LIB_DIR unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_INCLUDE_DIR
X86_64_PC_WINDOWS_GNU_OPENSSL_INCLUDE_DIR unset
cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
OPENSSL_INCLUDE_DIR unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_DIR
X86_64_PC_WINDOWS_GNU_OPENSSL_DIR unset
cargo:rerun-if-env-changed=OPENSSL_DIR
OPENSSL_DIR = C:\Users\Administrator\Desktop\wintun-main\OpenSSL-Win32
cargo:rustc-link-search=native=C:\Users\Administrator\Desktop\wintun-main\OpenSSL-Win32\lib
cargo:include=C:\Users\Administrator\Desktop\wintun-main\OpenSSL-Win32\include
cargo:rerun-if-changed=build/expando.c
OPT_LEVEL = Some("0")
TARGET = Some("x86_64-pc-windows-gnu")
HOST = Some("x86_64-pc-windows-gnu")
CC_x86_64-pc-windows-gnu = None
CC_x86_64_pc_windows_gnu = None
HOST_CC = None
CC = None
CFLAGS_x86_64-pc-windows-gnu = None
CFLAGS_x86_64_pc_windows_gnu = None
HOST_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
running: "gcc.exe" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-m64" "-I" "C:\\Users\\Administrator\\Desktop\\wintun-main\\OpenSSL-Win32\\include" "-Wall" "-Wextra" "-E" "build/expando.c"
exit code: 0
version: 3_0_2
cargo:rustc-cfg=osslconf="OPENSSL_NO_SSL3_METHOD"
cargo:conf=OPENSSL_NO_SSL3_METHOD
cargo:rustc-cfg=ossl300
cargo:rustc-cfg=ossl101
cargo:rustc-cfg=ossl102
cargo:rustc-cfg=ossl102f
cargo:rustc-cfg=ossl102h
cargo:rustc-cfg=ossl110
cargo:rustc-cfg=ossl110f
cargo:rustc-cfg=ossl110g
cargo:rustc-cfg=ossl110h
cargo:rustc-cfg=ossl111
cargo:rustc-cfg=ossl111b
cargo:rustc-cfg=ossl111c
cargo:version_number=30000020
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_LIBS
X86_64_PC_WINDOWS_GNU_OPENSSL_LIBS unset
cargo:rerun-if-env-changed=OPENSSL_LIBS
OPENSSL_LIBS unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_STATIC
cargo:rustc-cfg=ossl102f
cargo:rustc-cfg=ossl102h
cargo:rustc-cfg=ossl110
cargo:rustc-cfg=ossl110f
cargo:rustc-cfg=ossl110g
cargo:rustc-cfg=ossl110h
cargo:rustc-cfg=ossl111
cargo:rustc-cfg=ossl111b
cargo:rustc-cfg=ossl111c
cargo:version_number=30000020
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_LIBS
X86_64_PC_WINDOWS_GNU_OPENSSL_LIBS unset
cargo:rerun-if-env-changed=OPENSSL_LIBS
OPENSSL_LIBS unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_STATIC
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_LIBS
X86_64_PC_WINDOWS_GNU_OPENSSL_LIBS unset
cargo:rerun-if-env-changed=OPENSSL_LIBS
OPENSSL_LIBS unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_STATIC
X86_64_PC_WINDOWS_GNU_OPENSSL_STATIC unset
cargo:rerun-if-env-changed=OPENSSL_STATIC
OPENSSL_STATIC unset
--- stderr
thread 'main' panicked at 'OpenSSL libdir at `C:\Users\Administrator\Desktop\wintun-main\OpenSSL-Win32\lib` does not contain the required files to either statically or dynamically link OpenSSL', C:\Users\Administrator\.cargo\registry\src\github.com-1ecc6299db9ec823\openssl-sys-0.9.72\build/main.rs:367:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
I am not able to figure out what the issue is. I have installed openssl and mingw exe files and added paths to the environment variables as well as shown below.
I cloned your repo and was able to build it after a few steps. Below is what I did.
Install openssl for windows using the following link:
Win64 full install for software developers
Make sure the following environment variable is set:
OPENSSL_DIR=C:\Program Files\OpenSSL-Win64
Update the openssl cargo crate version to openssl = "0.10.38"

Yocto build fails due to conflict between recipes

I have two recipes, the first, adduser, is based on the useradd example in the meta-skeleton layer, and creates the directory /home/foo/, for the user foo:
SUMMARY = "Example recipe for using inherit useradd"
DESCRIPTION = "This recipe serves as an example for using features from useradd.bbclass"
SECTION = "examples"
PR = "r1"
LICENSE = "MIT"
S = "${WORKDIR}"
inherit useradd
# You must set USERADD_PACKAGES when you inherit useradd. This
# lists which output packages will include the user/group
# creation code.
USERADD_PACKAGES = "${PN}"
USERADD_PARAM_${PN} = "-d /home/foo -r -s /bin/bash -p 'bar' foo"
do_install () {
install -d -m 755 ${D}/usr/share/foo
install -d -m 755 ${D}/home/foo
chown -R foo ${D}/usr/share/foo
chown -R foo ${D}/home/foo
}
FILES_${PN} = "/usr/share/foo /home/foo"
# Prevents do_package failures with:
# debugsources.list: No such file or directory:
INHIBIT_PACKAGE_DEBUG_SPLIT = "1"
I then have a second recipe, which should add the directory "code" to the user's home directory, and put a file in it:
LICENSE = "CLOSED"
DEPENDS = "adduser"
FILES_${PN} += "/home/foo/code /home/foo/code/base_reading.py"
SRC_URI = "file://base_reading.py \
"
S = "${WORKDIR}"
do_install() {
install -d ${D}/home/foo/code
install -m 0755 ${S}/base_reading.py ${D}/home/foo/code/base_reading.py
chown -R foo ${D}/home/foo/code
}
This is the whole error: https://paste.ubuntu.com/p/6bfH4vf8qw but the TL;DR is:
Error: Transaction check error:
file /home/foo conflicts between attempted installs of basecode-1.0-r0.cortexa7hf_neon_vfpv4 and adduser-1.0-r1.cortexa7hf_neon_vfpv4
At first, I tried to fix this by adding the DEPENDS = "adduser", thinking that that would ensure that /home/foo exists before it tries to create /home/foo/code, but it didn't make any difference.

qbs avr compiling

I try to build simple project with qbs
import qbs
Project {
name: "simple"
Product {
name: "micro"
type: "obj"
Group {
name: "sources"
files: ["main.c", "*.h", "*.S"]
fileTags: ['c']
}
Rule {
inputs: ["c"]
Artifact {
fileTags: ['obj']
filePath: input.fileName + '.o'
}
prepare: {
var args = [];
args.push("-g")
args.push("-Os")
args.push("-w")
args.push("-fno-exceptions")
args.push("-ffunction-sections")
args.push("-fdata-sections")
args.push("-MMD")
args.push("-mmcu=atmega328p")
args.push("-DF_CPU=16000000L")
args.push("-DARDUINO=152")
args.push("-IC:/Programs/arduino/hardware/arduino/avr/cores/arduino")
args.push("-IC:/Programs/arduino/hardware/arduino/avr/variants/standard")
args.push("-c")
args.push(input.fileName)
args.push("-o")
args.push(input.fileName + ".o")
var compilerPath = "C:/Programs/arduino/hardware/tools/avr/bin/avr-g++.exe"
var cmd = new Command(compilerPath, args);
cmd.description = 'compiling ' + input.fileName;
cmd.highlight = 'compiler';
cmd.silent = false;
console.error(input.baseDir + '/' + input.fileName);
return cmd;
}
}
}
}
And I get error
compiling main.c
C:/Programs/arduino/hardware/tools/avr/bin/avr-g++.exe -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD "-mmcu=atmega328p" "-DF_CPU=16000000L" "-DARDUINO=152" -IC:/Programs/arduino/hardware/arduino/avr/cores/arduino -IC:/Programs/arduino/hardware/arduino/avr/variants/standard -c main.c -o main.c.o
avr-g++.exe: main.c: No such file or directory
avr-g++.exe: no input files
Process failed with exit code 1.
The following products could not be built for configuration qtc_avr_f84c45e7-release:
micro
What do I wrong?
File main.c present in project and in directory.
If I start this command from command prompt I do not get error.
In short, you need to pass input.filePath after -c and -o, not input.fileName. There's no guarantee that the working directory of the command invoked will be that of your source directory.
You can set the workingDirectory of a Command object, but that is not generally recommended as your commands should be independent of the working directory unless absolutely necessary.
Furthermore, you appear to be duplicating the functionality of the cpp module. Instead, your project should look like this:
import qbs
Project {
name: "simple"
Product {
Depends { name: "cpp" }
name: "micro"
type: "obj"
Group {
name: "sources"
files: ["main.c", "*.h", "*.S"]
fileTags: ['c']
}
cpp.debugInformation: true // passes -g
cpp.optimization: "small" // passes -Os
cpp.warningLevel: "none" // passes -w
cpp.enableExceptions: false // passes -fno-exceptions
cpp.commonCompilerFlags: [
"-ffunction-sections",
"-fdata-sections",
"-MMD",
"-mmcu=atmega328p"
]
cpp.defines: [
"F_CPU=16000000L",
"ARDUINO=152"
]
cpp.includePaths: [
"C:/Programs/arduino/hardware/arduino/avr/cores/arduino",
"C:/Programs/arduino/hardware/arduino/avr/variants/standard
]
cpp.toolchainInstallPath: "C:/Programs/arduino/hardware/tools/avr/bin"
cpp.cxxCompilerName: "avr-g++.exe"
}
}
it's work
args.push("-c")
args.push(input.filePath) at you args.push(input.fileName)
args.push("-o")
args.push(output.filePath)at you args.push(input.fileName)

shell "No such file or directory" on variable execution result

some basic function with goal to extract xml var by xpath:
function get_xml_value_from_config_dir {
local src_root=$1
local xpath_expr="//$2/text()"
local path_to_local="$src_root/app/etc/local.xml"
if [ ! -f $path_to_local ]; then echo "Config file not found: $path_to_local"; exit; fi;
echo $("$xmllint --nocdata --xpath '$xpath_expr' $path_to_local")
}
## and then
src_usr=$(get_xml_value_from_config_dir $src_dir username)
gives me
line 34: /usr/bin/xmllint --nocdata --xpath '//username/text()' /tmp/bin/app/etc/local.xml: No such file or directory
why? ( /usr/bin/xmllint exist as well as /tmp/bin/app/etc/local.xml )
It's telling you it can't find the file or directory named
/usr/bin/xmllint --nocdata --xpath '//username/text()' /tmp/bin/app/etc/local.xml
which indeed is unlikely to exist on your system.
Replace
echo $("$xmllint --nocdata --xpath '$xpath_expr' $path_to_local")
with
echo $($xmllint --nocdata --xpath "$xpath_expr" $path_to_local)
Incidentally, that will put all xmllint output on a single line; to avoid that, just use
xmllint --nocdata --xpath "$xpath_expr" $path_to_local

Getting error in hack/update-all.sh script

I have been trying to run ./hack/update-all.sh script and I am getting this error while updating codegen:
$ ./hack/update-all.sh
Running in the silent mode, run with -v if you want to see script logs.
Running in short-circuit mode; run with -a to force all scripts to run.
Updating generated-protobuf
Updating codegen
# runtime
/usr/local/go/src/runtime/os2_linux_generic.go:12: _SS_DISABLE redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:8
/usr/local/go/src/runtime/os2_linux_generic.go:13: _NSIG redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:9
/usr/local/go/src/runtime/os2_linux_generic.go:14: _SI_USER redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:10
/usr/local/go/src/runtime/os2_linux_generic.go:15: _SIG_BLOCK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:11
/usr/local/go/src/runtime/os2_linux_generic.go:16: _SIG_UNBLOCK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:12
/usr/local/go/src/runtime/os2_linux_generic.go:17: _SIG_SETMASK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:13
/usr/local/go/src/runtime/os2_linux_generic.go:18: _RLIMIT_AS redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:14
/usr/local/go/src/runtime/os2_linux_generic.go:24: sigset redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:20
/usr/local/go/src/runtime/os2_linux_generic.go:26: rlimit redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:22
/usr/local/go/src/runtime/panic1.go:11: paniclk redeclared in this block
previous declaration at /usr/local/go/src/runtime/panic.go:552
/usr/local/go/src/runtime/panic1.go:11: too many errors
!!! Error in /home/peeyush/work/kubernetes/hack/lib/golang.sh:435
'go install "${goflags[#]:+${goflags[#]}}" -ldflags "${goldflags}" "${nonstatics[#]:+${nonstatics[#]}}"' exited with status 2
Call stack:
1: /home/peeyush/work/kubernetes/hack/lib/golang.sh:435 kube::golang::build_binaries_for_platform(...)
2: /home/peeyush/work/kubernetes/hack/lib/golang.sh:574 kube::golang::build_binaries(...)
3: /home/peeyush/work/kubernetes/hack/build-go.sh:26 main(...)
Exiting with status 1
!!! Error in /home/peeyush/work/kubernetes/hack/lib/golang.sh:494
'( kube::golang::setup_env; echo "Go version: $(go version)"; local host_platform; host_platform=$(kube::golang::host_platform); local goflags goldflags; eval "goflags=(${KUBE_GOFLAGS:-})"; goldflags="${KUBE_GOLDFLAGS:-} $(kube::version::ldflags)"; local use_go_build; local -a targets=(); local arg; for arg in "$#";
do
if [[ "${arg}" == "--use_go_build" ]]; then
use_go_build=true;
else
if [[ "${arg}" == -* ]]; then
goflags+=("${arg}");
else
targets+=("${arg}");
fi;
fi;
done; if [[ ${#targets[#]} -eq 0 ]]; then
targets=("${KUBE_ALL_TARGETS[#]}");
fi; local -a platforms=("${KUBE_BUILD_PLATFORMS[#]:+${KUBE_BUILD_PLATFORMS[#]}}"); if [[ ${#platforms[#]} -eq 0 ]]; then
platforms=("${host_platform}");
fi; local binaries; binaries=($(kube::golang::binaries_from_targets "${targets[#]}")); local parallel=false; if [[ ${#platforms[#]} -gt 1 ]]; then
local gigs; gigs=$(kube::golang::get_physmem); if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then
kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"; parallel=true;
else
kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"; parallel=false;
fi;
fi; if [[ "${parallel}" == "true" ]]; then
kube::log::status "Building go targets for ${platforms[#]} in parallel (output will appear in a burst when complete):" "${targets[#]}"; local platform; for platform in "${platforms[#]}";
do
( kube::golang::set_platform_envs "${platform}"; kube::log::status "${platform}: go build started"; kube::golang::build_binaries_for_platform ${platform} ${use_go_build:-}; kube::log::status "${platform}: go build finished" ) &> "/tmp//${platform//\//_}.build" &
done; local fails=0; for job in $(jobs -p);
do
wait ${job} || let "fails+=1";
done; for platform in "${platforms[#]}";
do
cat "/tmp//${platform//\//_}.build";
done; exit ${fails};
else
for platform in "${platforms[#]}";
do
kube::log::status "Building go targets for ${platform}:" "${targets[#]}"; kube::golang::set_platform_envs "${platform}"; kube::golang::build_binaries_for_platform ${platform} ${use_go_build:-};
done;
fi )' exited with status 1
Call stack:
1: /home/peeyush/work/kubernetes/hack/lib/golang.sh:494 kube::golang::build_binaries(...)
2: /home/peeyush/work/kubernetes/hack/build-go.sh:26 main(...)
Exiting with status 1
!!! Error in ./hack/../hack/update-codegen.sh:32
'"${KUBE_ROOT}/hack/build-go.sh" ${BUILD_TARGETS[*]}' exited with status 1
Call stack:
1: ./hack/../hack/update-codegen.sh:32 main(...)
Exiting with status 1
Updating codegen FAILED
Any idea what could be the reason behind this? Or how to resolve this issue?
Looks like an environment issue. Cleared everything, cloned the repo afresh and everything is working fine.