Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: wait for windows app to exit as well #18

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
248604b
refactor: using c++ and fix several bugs about memory premature released
kiennq Mar 26, 2020
e5efe87
Makefile: optimize flag
kiennq Mar 27, 2020
7e47676
makefile: add static link flag
kiennq Apr 1, 2020
ca9edb7
Make 32bit binary
kiennq Apr 20, 2020
596ec97
bug: properly quote command arguments
kiennq May 10, 2020
a262d92
make: update version + make script for release
kiennq May 10, 2020
37746d0
makefile: update release file name
kiennq May 10, 2020
14efaee
fix: should also keep threadHandler alive until the end
kiennq May 10, 2020
eabc4f6
makefile: Using Oz flag
kiennq May 25, 2020
271b583
makefile: using LF file ending
kiennq Sep 29, 2020
f334a2c
makefile: using Ofast flag instead.
kiennq Oct 9, 2020
e20a3ac
Fix 'Could not create process with command' error
vieiraa Mar 1, 2022
f86c366
Merge pull request #2 from vieiraa/fix_space
kiennq Mar 30, 2022
db29490
ci: build automatically
kiennq Mar 30, 2022
54e6d9c
ci: use ninja instead of make, add compile test
kiennq Apr 1, 2022
99f7f73
Properly pass stdio handles to child process (#3)
cqjjjzr Apr 1, 2022
ea09917
Fix the detection of targets that use the GUI subsystem (#6)
pfmoore Aug 7, 2022
36a6c52
Add "shim:" at the start of errors (#8)
couleurm Mar 26, 2023
e0fb559
set application manifest to avoid unexpected elevation required
kiennq Mar 5, 2024
af19af4
Allow shim's dirname in path and args #1
kiennq Mar 6, 2024
964b259
Add version to ci
kiennq Mar 22, 2024
606aaa1
NormalizeArgs: fix case of can't find place holder
kiennq Mar 25, 2024
0e99827
build: Add zig build system support (#10)
chawyehsu Oct 30, 2024
66807e3
fix: wait for windows app as well
kiennq Nov 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: DontAlign
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: None

# Custom format to allow namespace and empty method compression.
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum : true
AfterExternBlock: true
AfterFunction: true
AfterNamespace: false
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false

BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
BreakStringLiterals: true
ColumnLimit: 160
CompactNamespaces: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerBinding: false
FixNamespaceComments: true
IncludeBlocks: Preserve
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
Language: Cpp
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
MacroBlockBegin: '^BEGIN_MODULE$|^BEGIN_TEST_CLASS$|^BEGIN_TEST_METHOD$|^BEGIN_COM_MAP$'
MacroBlockEnd: '^END_MODULE$|^END_TEST_CLASS$|^END_TEST_METHOD$|^END_TEST_METHOD$|^END_COM_MAP$'
7 changes: 7 additions & 0 deletions .github/scripts/setenv.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$_VsInstallerDir = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\";
if (Test-Path "$_VsInstallerDir\vswhere.exe") {
$env:PATH += ";$_VsInstallerDir";
$installPath = vswhere -latest -property installationPath;
Import-Module (Join-Path $installPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll") -Force;
Enter-VsDevShell -VsInstallPath $installPath -SkipAutomaticLocation;
}
26 changes: 26 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build

on: [push, pull_request]

jobs:
build:
runs-on: windows-latest
env:
MSYS2_PATH_TYPE: inherit
defaults:
run:
shell: pwsh

steps:
- uses: actions/checkout@v2
- uses: msys2/setup-msys2@v2
with:
update: false
release: false
install: >-
ninja

- name: Build
run: |
.github/scripts/setenv.ps1
ninja -C build shim.exe
68 changes: 68 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Release

on:
schedule:
- cron: "0 0 * * 0" # weekly
workflow_dispatch:
push:
paths:
- 'version'

jobs:
build:
runs-on: windows-latest
env:
MSYS2_PATH_TYPE: inherit
defaults:
run:
shell: msys2 {0}

steps:
- uses: msys2/setup-msys2@v2
with:
update: false
release: false
install: >-
zip
ninja
- uses: actions/checkout@v2

- name: Get latest release
continue-on-error: true
id: cur_release
uses: pozetroninc/github-action-get-latest-release@master
with:
repository: ${{ github.repository }}
excludes: draft

- name: Get version
run: |
base_ver=$(cat version)
sha=${GITHUB_SHA::7}
version=$([ -z "${{ github.event.schedule }}" ] && echo ${base_ver} || echo ${base_ver}.$sha)
cur_ver=$([[ ${{ steps.cur_release.outcome }} == failure ]] && echo v0 || echo ${{ steps.cur_release.outputs.release }})
[[ v$version == $cur_ver ]] && exit 1
prerelease=$([ -z "${{ github.event.schedule }}" ] && echo false || echo true)
echo "version=$version" >> $GITHUB_ENV
echo "prerelease=$prerelease" >> $GITHUB_ENV

- name: Setup environment
shell: pwsh
run: |
.github/scripts/setenv.ps1
echo $env:PATH | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

- name: Build
run: ninja -C build

- name: Create release
uses: ncipollo/release-action@v1
with:
tag: v${{ env.version }}
name: ${{ env.version }}
draft: false
prerelease: ${{ env.prerelease }}
artifactErrorsFailBuild: true
artifacts: build/shimexe.zip
artifactContentType: application/zip
token: ${{ secrets.GITHUB_TOKEN }}
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.vscode/
*.exe
*.obj
*.pdb
*.ilk
*.o
checksum.sha*
obj/
bin/
archive/
build/**
!build/*.ninja
!build/build.ps1
!build/build.zig
.zig-cache/
zig-out/
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
CC=clang++.exe
CFLAGS=-std=c++17 -m32

ODIR = obj
BDIR = bin
ADIR = archive

TARGET = $(BDIR)/shim.exe
OBJ = shim.o
OBJS = $(patsubst %,$(ODIR)/%,$(OBJ))

$(TARGET): $(OBJS) | $(BDIR)
$(CC) -o $(TARGET) $^ $(CFLAGS) -Ofast -static
sha256sum $(TARGET) > $(BDIR)/checksum.sha256
sha512sum $(TARGET) > $(BDIR)/checksum.sha512

$(ODIR)/%.o: %.cpp | $(ODIR)
$(CC) -c -o $@ $< $(CFLAGS) -Ofast -g

$(ODIR):
mkdir -p $(ODIR)

$(BDIR):
mkdir -p $(BDIR)

.PHONY: clean debug zip

clean:
rm -f $(ODIR)/*.*
rm -f $(BDIR)/*.*

debug: $(OBJS) | $(BDIR)
$(CC) -o $(BDIR)/shim.exe $^ $(CFLAGS) -g

$(ADIR):
mkdir -p $(ADIR)

$(ADIR)/shimexe.zip: $(TARGET) | $(ADIR)
cd $(ADIR) && zip -j -9 shimexe.zip ../$(BDIR)/*.*

zip: $(ADIR)/shimexe.zip
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ file to redirect commands from `scoop\shims\app.exe` to `scoop\apps\app\current\
The last issue making interaction with REPLs and long-running apps practically impossible,
and having never been fixed, I set out to improve them with this repository.

[`shim.c`](./shim.c) is:
[`shim.cpp`](./shim.cpp) is:
- **Faster**, because it does not use the .NET Framework, and parses the `.shim` file in a simpler way.
- **More efficient**, because by the time the target of the shim is started, all allocated memory will have been freed.
- And more importantly, it **works better**:
Expand All @@ -24,6 +24,7 @@ and having never been fixed, I set out to improve them with this repository.
## Installation

- In a Visual Studio command prompt, run `cl /O1 shim.c`.
- Or using `clang++` with `clang++ shim.cpp -o shim.exe -m32 -O -std=c++17 -g`.
- Replace any `.exe` in `scoop\shims` by `shim.exe`.

An additional script, `repshims.bat`, is provided. It will replace all `.exe`s in the user's Scoop directory
Expand Down
32 changes: 32 additions & 0 deletions build/build.ninja
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cc = clang++.exe
cflags = -std=c++20 -m32

rule cc
command = $cc -c -o $out $in $cflags -O2 -g

rule link
command = $cc -o $out $cflags -O2 -static -Wl,/manifest:embed -Wl,/manifestinput:$in

rule link-debug
command = $cc -o $out $cflags -O2 -static -fuse-ld=lld -g -Wl,/manifest:embed -Wl,/manifestinput:$in

rule mkdir
command = mkdir -p $in

rule sha256sum
command = sha256sum $in > $out

rule sha512sum
command = sha512sum $in > $out

rule zip
command = zip -j -9 $out $in

build shim.o: cc ../shim.cpp
build shim.exe: link ../shim.manifest shim.o
build shimd.exe: link-debug ../shim.manifest shim.o
build checksum.sha256: sha256sum shim.exe
build checksum.sha512: sha512sum shim.exe
build shimexe.zip: zip shim.exe checksum.sha256 checksum.sha512

default shimexe.zip
94 changes: 94 additions & 0 deletions build/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env pwsh
#Requires -Version 7

<#
.SYNOPSIS
Build shim.exe using Zig.
.PARAMETER BuildMode
The build mode. Valid values are Debug, ReleaseSafe, ReleaseFast, ReleaseSmall
Default is ReleaseSmall
.PARAMETER Target
The target architecture. Valid values are x86-windows-gnu, x86_64-windows-gnu, aarch64-windows-gnu
Default is undefined (all valid targets)
.PARAMETER Zip
Generate checksums and pack the artifacts into a zip file for distribution
#>
param(
[ValidateSet('Debug', 'ReleaseSafe', 'ReleaseFast', 'ReleaseSmall')]
[string]$BuildMode = "ReleaseSmall",
[ValidateSet('x86-windows-gnu', 'x86_64-windows-gnu', 'aarch64-windows-gnu')]
[string]$Target,
[switch]$Zip = $false
)

$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"

if (-not [bool](Get-Command zig -ErrorAction SilentlyContinue)) {
Write-Host "Zig is not installed. Please install Zig before running this script." -ForegroundColor Yellow
exit 1
}

Remove-Item -Path "$PSScriptRoot\zig-out" -Recurse -Force -ErrorAction SilentlyContinue

Push-Location $PSScriptRoot

if (-not $Target -or $Target -eq 'x86-windows-gnu') {
Write-Host "Build shim.exe for x86-windows-gnu target..." -ForegroundColor Cyan
Start-Process -FilePath "zig" -ArgumentList "build -Dtarget=x86-windows-gnu -Doptimize=$BuildMode" -Wait -NoNewWindow
Rename-Item -Path "$PSScriptRoot\zig-out\bin\shim.exe" -NewName "$PSScriptRoot\zig-out\bin\shim-ia32.exe"
}

if (-not $Target -or $Target -eq 'x86_64-windows-gnu') {
Write-Host "Build shim.exe for x86_64-windows-gnu target..." -ForegroundColor Cyan
Start-Process -FilePath "zig" -ArgumentList "build -Dtarget=x86_64-windows-gnu -Doptimize=$BuildMode" -Wait -NoNewWindow
Rename-Item -Path "$PSScriptRoot\zig-out\bin\shim.exe" -NewName "$PSScriptRoot\zig-out\bin\shim-amd64.exe"
}

if (-not $Target -or $Target -eq 'aarch64-windows-gnu') {
Write-Host "Build shim.exe for aarch64-windows-gnu target..." -ForegroundColor Cyan
Start-Process -FilePath "zig" -ArgumentList "build -Dtarget=aarch64-windows-gnu -Doptimize=$BuildMode" -Wait -NoNewWindow
Rename-Item -Path "$PSScriptRoot\zig-out\bin\shim.exe" -NewName "$PSScriptRoot\zig-out\bin\shim-aarch64.exe"
}

if ($Zip) {
Write-Host "Generate checksums..." -ForegroundColor Cyan

# shim-ia32.exe
if (-not $Target -or $Target -eq 'x86-windows-gnu') {
$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-ia32.exe" -Algorithm SHA256).Hash.ToLower()
"$sha256 shim-ia32.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-ia32.exe.sha256"
$sha512 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-ia32.exe" -Algorithm SHA512).Hash.ToLower()
"$sha512 shim-ia32.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-ia32.exe.sha512"
}

# shim-amd64.exe
if (-not $Target -or $Target -eq 'x86_64-windows-gnu') {
$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-amd64.exe" -Algorithm SHA256).Hash.ToLower()
"$sha256 shim-amd64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-amd64.exe.sha256"
$sha512 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-amd64.exe" -Algorithm SHA512).Hash.ToLower()
"$sha512 shim-amd64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-amd64.exe.sha512"
}

# shim-aarch64.exe
if (-not $Target -or $Target -eq 'aarch64-windows-gnu') {
$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-aarch64.exe" -Algorithm SHA256).Hash.ToLower()
"$sha256 shim-aarch64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-aarch64.exe.sha256"
$sha512 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-aarch64.exe" -Algorithm SHA512).Hash.ToLower()
"$sha512 shim-aarch64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-aarch64.exe.sha512"
}

Write-Host "Packaging..." -ForegroundColor Cyan

$version = (Get-Content "$PSScriptRoot\..\version").Trim()
Compress-Archive -Path "$PSScriptRoot\zig-out\bin\shim-*" -DestinationPath "$PSScriptRoot\zig-out\shimexe-$version.zip"

$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\shimexe-$version.zip" -Algorithm SHA256).Hash.ToLower()
"$sha256 shimexe-$version.zip" | Out-File "$PSScriptRoot\zig-out\shimexe-$version.zip.sha256"
}

Write-Host "Artifacts available in $PSScriptRoot\zig-out" -ForegroundColor Green

Pop-Location

$ErrorActionPreference = $oldErrorActionPreference
Loading