TransWikia.com

Capture output of powershell command in makefile on windows

Super User Asked by qwertycomputer on December 26, 2020

I’m trying to create a Makefile that will work on both windows and unix-based operating systems. (For reference, the windows version of make it should work with can be found here). As part of the Makefile, I need to be able to split a path (contained in an environment variable) to get just the filename (with extension). I use the command "basename" for non-windows OS’s, and am currently trying to use the Powershell command "Split-Path" to get the file name from the path on Windows. Running this command in the command line or in a make recipe gives the desired output. However, I can’t get it to work so that I can capture the results of the Powershell command within the Makefile to a variable. My current setup in the Makefile to detect the operating system and get the file from the path can be seen below:

ifeq ($(OS),Windows_NT)
    # Windows
    # The following line doesn't capture the output as expected
    FILENAME_I_WANT:=$(powershell -noprofile Split-Path $(PATH_ENVIRONMENT_VARIABLE) -leaf)
else
    # Unix based OS
    FILENAME_I_WANT:=$(shell basename $(PATH_ENVIRONMENT_VARIABLE))
endif

Does anyone see anything I’m doing wrong, or have any suggestions for how to fix this?

Thanks!

One Answer

Why are you not using the environment variables for OS, and just dot referencing the basename of the filename?

# Get the host OS
$env:OS
# Results
<#
Windows_NT
#>

# Get the target 5th in the folder and only show the basename
((Get-ChildItem -Path 'D:Temp')[5]).BaseName
# Results
<#
IEDriverServer_Win32_3.150.1
#>

Answered by postanote on December 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP