Skip to content

Commit

Permalink
Change build process: Add Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Aug 13, 2023
1 parent e2c0d6b commit 2c2fc66
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 79 deletions.
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ todo*
AutoHotkey*.chm
.vscode/.history

build/libxdo.so*
build/libxkbcommon.so*
build/libxkbcustom.a
build/linuxdeploy-x86_64.AppImage
build/linuxdeploy-plugin-gtk.sh
build/*.AppImage*
linuxdeploy-x86_64.AppImage
linuxdeploy-plugin-gtk.sh
*.AppImage*

# for generating a .dockerignore based on .gitignore:
.git
97 changes: 97 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
.PHONY: all clean test-appimage test-dev install uninstall install install-bin install-assets install-appimage install-bin-appimage
PREFIX ?= /usr

all:
echo Please specify target.

download-appimage:
curl -s https://api.github.com/repos/phil294/ahk_x11/releases/latest \
| grep "browser_download_url.*/ahk_x11-.\..\..-x86_64.AppImage" \
| cut -d : -f 2,3 \
| tr -d \" \
| wget -i - -O ahk_x11.AppImage

ahk_x11.AppImage: bin/ahk_x11 linuxdeploy-plugin-gtk.sh linuxdeploy-x86_64.AppImage
(cat /etc/lsb-release |grep "Ubuntu 20.04") || \
(echo "It seems you're NOT on Ubuntu 20.04. AppImages should be build on that distribution for cross-distro compatibility. Please change your system e.g. by running all of this in a 20.04 Docker container. Alternatively, you can build for your local system only with the bin/ahk_x11 make target (not recommended)."; exit 1)
rm -rf AppDir
# libthai: https://github.com/phil294/AHK_X11/issues/45 https://github.com/AppImageCommunity/pkg2appimage/issues/538
# Other libraries that are missing on docker debian and frolvlad/alpine-glibc but are on excludelist
# https://github.com/AppImageCommunity/pkg2appimage/blob/master/excludelist)
# ...but that haven't caused problems so far: libfontconfig.so.1, libfribidi.so.0, libharfbuzz.so.0, libgpg-error.so.0(only alpine)
OUTPUT=ahk_x11.AppImage ./linuxdeploy-x86_64.AppImage --appdir AppDir --output appimage --desktop-file ./assets/ahk_x11.desktop --icon-file ./assets/ahk_x11.png --executable ./bin/ahk_x11 --library /usr/lib/x86_64-linux-gnu/libthai.so.0 --plugin gtk
rm -rf AppDir

bin/ahk_x11:
ifneq ($(MAKECMDGOALS), ahk_x11.AppImage)
@echo -e "WARNING: You are building the native release binary WITHOUT AppImage wrapper. The resulting program will work but not be very portable as dependencies are not bundled. This means that if you use AHK_X11's COMPILER FEATURE to bundle a script into a standalone binary, this binary will then VERY LIKELY NOT RUN ON OTHER LINUX SYSTEMS, or may fail to run on your system in the future. \n\nIt is highly recommended you make the "ahk_x11.AppImage" target instead. The file output size will be three times larger but eternally portable."
endif
$(MAKE) bin/ahk_x11.dev BUILD_EXTRA_ARGS="--release"
mv bin/ahk_x11.dev bin/ahk_x11

# Without the --release argument, this adds debug symbols and is slow at runtime
bin/ahk_x11.dev: lib/configured xdotool/libxdo.a
# We always link libxdo statically even when we're not using AppImage as we need
# the recent fixes from master branch, also its abi bump, we depend on v2021+.
# Xinerama etc. are its dependencies - these we keep dynamic.
# Cannot overwrite CRYSTAL_LIBRARY_PATH because crystal#12380, need link-flag instead.
shards build -Dpreview_mt --link-flags="-no-pie \
-L'${PWD}/xdotool' \
-Wl,-Bstatic -lxdo \
-Wl,-Bdynamic -lxkbcommon -lXinerama -lXext -lXtst -lXi" \
$(BUILD_EXTRA_ARGS)
mv bin/ahk_x11 bin/ahk_x11.dev

linuxdeploy-plugin-gtk.sh linuxdeploy-x86_64.AppImage:
wget https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh
# Custom themes are broken https://github.com/linuxdeploy/linuxdeploy-plugin-gtk/issues/39
sed -i -E 's/export GTK_THEME=/# /' linuxdeploy-plugin-gtk.sh
wget https://github.com/linuxdeploy/linuxdeploy/releases/latest/download/linuxdeploy-x86_64.AppImage
chmod +x linuxdeploy-*

xdotool/.git:
git submodule update --init

xdotool/libxdo.a: xdotool/.git
$(MAKE) -C xdotool libxdo.a

lib/configured:
shards install
./bin/gi-crystal
# TODO: can be fixed in application code?
sed -i -E 's/private getter xdo_p/getter xdo_p/' lib/x_do/src/x_do.cr
# https://github.com/hugopl/gi-crystal/issues/80
sed -i -E 's/GLib::String/::String/g' lib/gi-crystal/src/auto/gtk-3.0/gtk.cr
touch lib/configured

test-appimage: ahk_x11.AppImage
./ahk_x11.AppImage tests.ahk
test-dev: bin/ahk_x11
./bin/ahk_x11 tests.ahk

clean:
rm -rf ahk_x11.AppImage bin/ahk_x11 bin/ahk_x11.dev linuxdeploy-plugin-gtk.sh linuxdeploy-x86_64.AppImage lib
git submodule deinit --all

install: install-bin install-assets
install-appimage: install-bin-appimage install-assets
install-bin:
install -D -m 0755 bin/ahk_x11 $(DESTDIR)$(PREFIX)/bin/
install-bin-appimage:
install -D -m 0755 ahk_x11.AppImage $(DESTDIR)$(PREFIX)/bin/ahk_x11
install-assets:
install -D -m 0644 assets/ahk_x11.png $(DESTDIR)$(PREFIX)/share/icons/hicolor/48x48/mimetypes/application-x-ahk_x11.png
install -D -m 0644 assets/ahk_x11-mime.xml $(DESTDIR)$(PREFIX)/share/mime/packages/
install -D -m 0644 assets/ahk_x11.desktop $(DESTDIR)$(PREFIX)/share/applications/
install -D -m 0644 assets/ahk_x11-compiler.desktop $(DESTDIR)$(PREFIX)/share/applications/
install -D -m 0644 assets/ahk_x11-windowspy.desktop $(DESTDIR)$(PREFIX)/share/applications/
# Setting a default ahk_x11.desktop for mime application/x-ahk_x11 seems to not really be possible by standard and it also would be too intrusive probably

uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/ahk_x11*
rm -f $(DESTDIR)$(PREFIX)/share/icons/hicolor/48x48/mimetypes/application-x-ahk_x11.png
rm -f $(DESTDIR)$(PREFIX)/share/mime/packages/ahk_x11-mime.xml
rm -f $(DESTDIR)$(PREFIX)/share/mime/application/x-ahk_x11.xml
rm -f $(DESTDIR)$(PREFIX)/share/applications/ahk_x11.desktop
rm -f $(DESTDIR)$(PREFIX)/share/applications/ahk_x11-compiler.desktop
rm -f $(DESTDIR)$(PREFIX)/share/applications/ahk_x11-windowspy.desktop
8 changes: 8 additions & 0 deletions assets/ahk_x11-mime.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/x-ahk_x11">
<comment>AHK_X11: AutoHotkey for Linux</comment>
<icon name="application-x-ahk_x11"/>
<glob pattern="*.ahk"/>
</mime-type>
</mime-info>
26 changes: 8 additions & 18 deletions build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,24 @@ These are the steps required to build this project locally, such as if you want
```
1. `git clone https://github.com/phil294/AHK_X11`
1. `cd AHK_X11`
1. `shards install`
1. `bin/gi-crystal`
1. Remove the `private` from `private getter xdo_p : LibXDo::XDo*` in `lib/x_do/src/x_do.cr` (this is a temporary fix)
1. In `lib/gi-crystal/src/auto/gtk-3.0/gtk.cr`, replace all usages of `Glib::String` with `::String` (this is a temporary fix)
1. Now everything is ready for local use with `shards build -Dpreview_mt`, *if* you have `libxdo` (xdotool) version 2021* upwards installed. For version 2016*, you'll need to upgrade this dependency somehow. One way to achieve this is explained below.
1. Find your final binary in the `./bin` folder, it's about 4 MiB in size.
1. `make bin/ahk_x11.dev`
1. Find your final binary in the `./bin` folder, it's about 13 MiB in size. It's not optimized for speed yet. Please also note that if you compile an `.ahk` script with it, it will NOT be portable across systems! For that, read on below.

### For making release-like binaries

The released binaries are special because they need to be portable. We achieve this by using AppImage. Portability is especially important because of the script compilation feature: You can use the binary to transform a script into a new stand-alone binary, and that resulting binary should be portable across various Linux distributions without ever requiring the user to install any dependencies. Below are the instructions on how to do this / how the released binaries are produced.
The released binaries are special because they need to be portable. We achieve this by using AppImage. Portability is especially important because of the script compilation feature: You can use the binary to transform a script into a new stand-alone binary, and that resulting binary should be runnable in the future and across various Linux distributions without ever requiring the user to install any dependencies. Below are the instructions on how to do this / how the released binaries are produced.

1. Get on an Ubuntu 20.04 system, e.g. using Docker. 18.04 also works but Gtk 3.24 in 20.04 fixes the bug that ToolTips falsely grab focus
1. `libxdo`'s master branch includes changes not present in the recent releases, so we need to build it by hand:

- clone [xdotool](https://github.com/jordansissel/xdotool) somewhere, in there,
- run `make clean && make` and then copy the files `libxdo.so` and `libxdo.so.3` into this very `build` folder.
1. Do the same steps as listed in "For local usage": Install dependencies etc.
1. Get `linuxdeploy-x86_64.AppImage` from https://github.com/linuxdeploy/linuxdeploy/releases, into this `build` folder
1. Get `linuxdeploy-plugin-gtk.sh` from https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh
1. In that same file, *delete the line* `export GTK_THEME="$APPIMAGE_GTK_THEME" # Custom themes are broken` (this is a temporary fix ([issue](https://github.com/linuxdeploy/linuxdeploy-plugin-gtk/issues/39)))
1. Instead of `shards build`, run `./build.sh --release`. This also does shards build, but also does the AppImage magic. The `--release` flag results in slower compilation but faster output binary.
1. Find your final binary `ahk_x11-[version]-x86_64.AppImage` in the `build` folder. It's about 30 MiB in size.
1. Run `make ahk_x11.AppImage`
1. Find your final binary as `ahk_x11.AppImage`. It's about 30 MiB in size.
1. You can then optionally either install it as usual for the current user by running directly *or* system-wide with `make install-appimage`. If you do the latter: Depending on your distribution, you might need to update the mime and desktop database with `sudo -i bash -c 'umask 0022 && update-mime-database /usr/share/mime && update-desktop-database'`.

There's a script to call `./build.sh`, make a new release and publish it etc., it's `../release.sh`. You most likely can't run it yourself though.
There's a script that does these things, makes a new release and publishes it etc., it's `./release.sh`. You most likely can't run it yourself though.

### Docker

In the rare case that you want to use ahk_x11 containerized for headless purposes, you can find a working Dockerfile example in `./ahk_x11.alpine.Dockerfile`. Run it in the parent (main) directory like so:
In the rare case that you want to use ahk_x11 containerized for headless purposes, you can find a working Dockerfile example in `./ahk_x11.alpine.Dockerfile`. Build it in the parent (main) directory like so:

```bash
cp .gitignore .dockerignore && \
Expand Down
16 changes: 2 additions & 14 deletions build/ahk_x11.alpine.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
FROM alpine:latest as build-xdotool
RUN apk add --no-cache alpine-sdk libxtst-dev libxinerama-dev libxkbcommon-dev libx11-dev
RUN git clone --depth=1 https://github.com/jordansissel/xdotool /xdo
WORKDIR /xdo
RUN make WITHOUT_RPATH_FIX=1 libxdo.a

FROM crystallang/crystal:latest-alpine AS build-ahkx11
RUN apk add --no-cache gtk+3.0-dev gobject-introspection-dev libxtst-dev libnotify-dev
RUN apk add --no-cache gtk+3.0-dev gobject-introspection-dev libxtst-dev libnotify-dev alpine-sdk libxinerama-dev libxkbcommon-dev libx11-dev
RUN git clone --depth=1 https://github.com/phil294/ahk_x11 /ahk
WORKDIR /ahk
RUN shards install
RUN bin/gi-crystal
RUN sed -i -E 's/private getter xdo_p/getter xdo_p/' lib/x_do/src/x_do.cr
RUN sed -i -E 's/GLib::String/::String/g' lib/gi-crystal/src/auto/gtk-3.0/gtk.cr
RUN mkdir static
COPY --from=build-xdotool /xdo/libxdo.a static
RUN shards build -Dpreview_mt --link-flags="-L$PWD/static -lxdo -lxkbcommon -lXinerama -lXtst -lXi -lX11" --release
RUN make bin/ahk_x11.release

FROM alpine:latest
RUN apk add --no-cache libx11 libnotify gtk+3.0 libgcc
Expand Down
31 changes: 0 additions & 31 deletions build/build.sh

This file was deleted.

13 changes: 8 additions & 5 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ pause

version=$(shards version)

docker run --rm -it -v /b/ahk_x11:/a --privileged ahk_x11-builder-ubuntu.20.04 bash -c \
'cd /a/build && ./build.sh --release'
docker run --rm -it -v /b/ahk_x11:/a -w /a --privileged ahk_x11-builder-ubuntu.20.04 \
make ahk_x11.AppImage

bin=$(ls -tr build/*.AppImage | tail -1)
bin=ahk_x11.AppImage
cp "$bin" "$bin.release"
echo "$bin"
ls -la "$bin"
pause

"$bin" ./tests.ahk
make test-appimage
pause

echo test installers
pause

sc git fetch
Expand Down
16 changes: 11 additions & 5 deletions src/installer.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ Echo, Starting graphical installer. Please see --help for more options.

If A_IsAdmin = 1
{
MsgBox, It seems you are running this installer for AHK_X11 with administrator permission. This is currently not supported, so please don't do that.`n`nThis means you probably ran the installer with "sudo". Please omit the sudo and try again.`n`nThe installer will exit now.
MsgBox, It seems you are running this installer for AHK_X11 with administrator permission. This is currently not supported, so please don't do that. For a system-wide install, clone and install from the repository.`n`nThis means you probably ran the installer with "sudo". Please omit the sudo and try again.`n`nThe installer will exit now.
ExitApp
}
IfExist, /usr/bin/ahk_x11
{
MsgBox, 4,, It seems like AHK_X11 is already installed globally. By continuing, this version will also be installed, but locally for the current user. Two simultaneous installations ins probably a bad idea. Are you sure you want to continue?
IfMsgBox, No
ExitApp
}

app_name = ahk_x11
app_ext = ahk
Expand All @@ -31,11 +37,11 @@ Install:
FileCreateDir, %binary_dir%
if binary_path = %A_ScriptFullPath%
{
MsgBox, 4,, It looks like you are trying to install the very same version that is already running. Are you sure you want to continue?
IfMsgBox, No
ExitApp
MsgBox, 4,, It looks like you are trying to install the very same version that is already running. Are you sure you want to continue?
IfMsgBox, No
ExitApp
} else {
FileCopy, %A_ScriptFullPath%, %binary_path%, 1
FileCopy, %A_ScriptFullPath%, %binary_path%, 1
}

; Icon
Expand Down

0 comments on commit 2c2fc66

Please sign in to comment.