Skip to content

Commit

Permalink
Embed menu icon in macOS executable
Browse files Browse the repository at this point in the history
  • Loading branch information
thevindu-w committed Nov 13, 2024
1 parent 7f9ecf8 commit 53337e0
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 53 deletions.
18 changes: 10 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
winres/clip_icon.ico -text

winres/app.rc -crlf
*.[pP][sS]1 -crlf
*.[cC][mM][dD] -crlf
*.[bB][aA][tT] -crlf
*.[iI][cC][sS] -crlf
* text=auto

*.sh text eol=lf
*.ico -merge -text
*.png -merge -text

*.[pP][sS]1 text eol=crlf
*.[cC][mM][dD] text eol=crlf
*.[bB][aA][tT] text eol=crlf
*.[iI][cC][sS] text eol=crlf

*.sh text eol=lf
*.html text eol=lf
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Check C code style
run: |
cpplint --linelength=120 --filter=-runtime/int,-runtime/arrays,-readability/casting,-legal/copyright \
--extensions=c,h,m,cc,hh,cpp,hpp,c++,h++,cxx,hxx --recursive .
--extensions=c,h,m,cc,hh,cpp,hpp,c++,h++,cxx,hxx --exclude='res/**/*_.c' --recursive .
- name: Check test script style
if: ${{ success() || failure() }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Check app.rc version
run: |
[ $(grep "${{env.VERSION}}" winres/app.rc | grep -iE '(File|Product)Version' | wc -l) = 4 ]
[ $(grep "${{env.VERSION}}" res/win/app.rc | grep -iE '(File|Product)Version' | wc -l) = 4 ]
- name: Check release_notes.md version
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ clip_share_web
*.tar
*.exe
*.res
res/**/*_.c
/*.conf
/allowed_clients.txt
server_err.log
Expand Down
42 changes: 28 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ CC=gcc
CFLAGS=-c -pipe -I. --std=gnu11 -fstack-protector -fstack-protector-all -Wall -Wextra -Wdouble-promotion -Wformat=2 -Wformat-nonliteral -Wformat-security -Wnull-dereference -Winit-self -Wmissing-include-dirs -Wswitch-default -Wstrict-overflow=4 -Wconversion -Wfloat-equal -Wshadow -Wpointer-arith -Wundef -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wredundant-decls -Wnested-externs -Woverlength-strings
CFLAGS_DEBUG=-g -DDEBUG_MODE

OBJS=main.o servers/clip_share.o servers/udp_serve.o proto/server.o proto/versions.o proto/methods.o utils/utils.o utils/net_utils.o utils/list_utils.o utils/config.o utils/kill_others.o
OBJS_C=main.o servers/clip_share.o servers/udp_serve.o proto/server.o proto/versions.o proto/methods.o utils/utils.o utils/net_utils.o utils/list_utils.o utils/config.o utils/kill_others.o

_WEB_OBJS_C=servers/clip_share_web.o
_WEB_OBJS_S=servers/page_blob.o
OBJS_M=
OBJS_BIN=

OTHER_DEPENDENCIES=
LINK_FLAGS_BUILD=
Expand All @@ -44,19 +45,19 @@ else
endif

ifeq ($(detected_OS),Linux)
OBJS+= xclip/xclip.o xclip/xclib.o xscreenshot/xscreenshot.o
OBJS_C+= xclip/xclip.o xclip/xclib.o xscreenshot/xscreenshot.o
CFLAGS+= -ftree-vrp -Wformat-signedness -Wshift-overflow=2 -Wstringop-overflow=4 -Walloc-zero -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wjump-misses-init -Wlogical-op -Wvla-larger-than=65536
CFLAGS_OPTIM=-Os
LDLIBS_NO_SSL=-lunistring -lX11 -lXmu -lXt -lxcb -lxcb-randr -lpng
LDLIBS_SSL=-lssl -lcrypto
LINK_FLAGS_BUILD=-no-pie -Wl,-s,--gc-sections
else ifeq ($(detected_OS),Windows)
OBJS+= utils/win_image.o
OBJS_C+= utils/win_image.o
CFLAGS+= -ftree-vrp -Wformat-signedness -Wshift-overflow=2 -Wstringop-overflow=4 -Walloc-zero -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wjump-misses-init -Wlogical-op -Wvla-larger-than=65536
CFLAGS+= -D__USE_MINGW_ANSI_STDIO
CFLAGS_OPTIM=-O3
OTHER_DEPENDENCIES+= winres/app.res
LDLIBS_NO_SSL=-l:libunistring.a -l:libpthread.a -lws2_32 -lgdi32 -l:libpng16.a -l:libz.a -lcrypt32 -lShcore -lUserenv
OTHER_DEPENDENCIES+= res/win/app.res
LDLIBS_SSL=-l:libssl.a -l:libcrypto.a -l:libpthread.a
LINK_FLAGS_BUILD=-no-pie -mwindows
PROGRAM_NAME:=$(PROGRAM_NAME).exe
Expand All @@ -66,6 +67,7 @@ else ifeq ($(detected_OS),Darwin)
export CPATH=$(shell brew --prefix)/include
export LIBRARY_PATH=$(shell brew --prefix)/lib
OBJS_M=utils/mac_utils.o utils/mac_menu.o
OBJS_BIN+= res/mac/icon.o
CFLAGS+= -fobjc-arc
CFLAGS_OPTIM=-O3
LDLIBS_NO_SSL=-framework AppKit -lunistring -lpng -lobjc
Expand All @@ -78,53 +80,65 @@ CFLAGS+= -DINFO_NAME=\"$(INFO_NAME)\" -DPROTOCOL_MIN=$(MIN_PROTO) -DPROTOCOL_MAX
CFLAGS_OPTIM+= -Werror

# append '_web' to objects for clip_share_web to prevent overwriting objects for clip_share
WEB_OBJS_C=$(OBJS:.o=_web.o) $(_WEB_OBJS_C:.o=_web.o)
WEB_OBJS_C=$(OBJS_C:.o=_web.o) $(_WEB_OBJS_C:.o=_web.o)
WEB_OBJS_S=$(_WEB_OBJS_S:.o=_web.o)
WEB_OBJS_M=$(OBJS_M:.o=_web.o)
WEB_OBJS=$(WEB_OBJS_C) $(WEB_OBJS_S) $(WEB_OBJS_M)
WEB_OBJS_BIN=$(OBJS_BIN:.o=_web.o)
WEB_OBJS=$(WEB_OBJS_C) $(WEB_OBJS_S) $(WEB_OBJS_M) $(WEB_OBJS_BIN)

# append '_debug' to objects for clip_share debug executable to prevent overwriting objects for clip_share
DEBUG_OBJS_C=$(OBJS:.o=_debug.o) $(_WEB_OBJS_C:.o=_debug.o)
DEBUG_OBJS_C=$(OBJS_C:.o=_debug.o) $(_WEB_OBJS_C:.o=_debug.o)
DEBUG_OBJS_M=$(OBJS_M:.o=_debug.o)
DEBUG_OBJS=$(DEBUG_OBJS_C) $(DEBUG_OBJS_M)
DEBUG_OBJS_BIN=$(OBJS_BIN:.o=_debug.o)
DEBUG_OBJS=$(DEBUG_OBJS_C) $(DEBUG_OBJS_M) $(DEBUG_OBJS_BIN)

# append '_no_ssl' to objects for clip_share_no_ssl executable to prevent overwriting objects for clip_share
NO_SSL_OBJS_C=$(OBJS:.o=_no_ssl.o) # Web mode is not supported with no_ssl
NO_SSL_OBJS_C=$(OBJS_C:.o=_no_ssl.o) # Web mode is not supported with no_ssl
NO_SSL_OBJS_M=$(OBJS_M:.o=_no_ssl.o)
NO_SSL_OBJS=$(NO_SSL_OBJS_C) $(NO_SSL_OBJS_M)
NO_SSL_OBJS_BIN=$(OBJS_BIN:.o=_no_ssl.o)
NO_SSL_OBJS=$(NO_SSL_OBJS_C) $(NO_SSL_OBJS_M) $(NO_SSL_OBJS_BIN)

$(PROGRAM_NAME): $(OBJS) $(OBJS_M) $(OTHER_DEPENDENCIES)
OBJS=$(OBJS_C) $(OBJS_M) $(OBJS_BIN)

$(PROGRAM_NAME): $(OBJS) $(OTHER_DEPENDENCIES)
$(PROGRAM_NAME_WEB): $(WEB_OBJS) $(OTHER_DEPENDENCIES)
$(PROGRAM_NAME) $(PROGRAM_NAME_WEB):
$(CC) $^ $(LINK_FLAGS_BUILD) $(LDLIBS) -o $@

$(PROGRAM_NAME_NO_SSL): $(NO_SSL_OBJS) $(OTHER_DEPENDENCIES)
$(CC) $^ $(LINK_FLAGS_BUILD) $(LDLIBS_NO_SSL) -o $@

$(OBJS): %.o: %.c
$(OBJS_C): %.o: %.c
$(OBJS_M): %.o: %.m
$(OBJS) $(OBJS_M):
$(OBJS_BIN): %.o: %_.c
$(OBJS) res/mac/icon.o:
$(CC) $(CFLAGS_OPTIM) $(CFLAGS) -fno-pie $^ -o $@

$(WEB_OBJS_C): %_web.o: %.c
$(WEB_OBJS_M): %_web.o: %.m
$(WEB_OBJS_S): %_web.o: %.S
$(WEB_OBJS_BIN): %_web.o: %_.c
$(WEB_OBJS):
$(CC) $(CFLAGS_OPTIM) $(CFLAGS) -DWEB_ENABLED -fno-pie $^ -o $@

$(DEBUG_OBJS_C): %_debug.o: %.c
$(DEBUG_OBJS_M): %_debug.o: %.m
$(DEBUG_OBJS_BIN): %_debug.o: %_.c
$(DEBUG_OBJS):
$(CC) $(CFLAGS) $(CFLAGS_DEBUG) $^ -o $@

$(NO_SSL_OBJS_C): %_no_ssl.o: %.c
$(NO_SSL_OBJS_M): %_no_ssl.o: %.m
$(NO_SSL_OBJS_BIN): %_no_ssl.o: %_.c
$(NO_SSL_OBJS):
$(CC) $(CFLAGS_OPTIM) $(CFLAGS) -DNO_SSL -fno-pie $^ -o $@

winres/app.res: winres/app.rc winres/resource.h
res/win/app.res: res/win/app.rc res/win/resource.h
windres -I. $< -O coff -o $@

res/mac/icon_.c: res/mac/icon.png
xxd -i $< >$@

.PHONY: all clean debug web test check install

all: $(PROGRAM_NAME) $(PROGRAM_NAME_WEB)
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
#include <pwd.h>
#include <sys/wait.h>
#elif defined(_WIN32)
#include <res/win/resource.h>
#include <shellapi.h>
#include <shellscalingapi.h>
#include <tlhelp32.h>
#include <userenv.h>
#include <winres/resource.h>
#elif defined(__APPLE__)
#include <pwd.h>
#include <utils/mac_menu.h>
Expand Down
Binary file added res/mac/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 24 additions & 24 deletions winres/app.rc → res/win/app.rc
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#include <winres/resource.h>
APP_ICON ICON "clip_icon.ico"
1 VERSIONINFO
FILEVERSION 3,5,0,0
PRODUCTVERSION 3,5,0,0
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
BEGIN
VALUE "FileDescription", "Clipboard Share"
VALUE "FileVersion", "3.5.0"
VALUE "InternalName", "clip_share"
VALUE "LegalCopyright", "H. Thevindu J. Wijesekera"
VALUE "OriginalFilename", "clip_share.exe"
VALUE "ProductName", "ClipShare"
VALUE "ProductVersion", "3.5.0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x809, 1252
END
#include <res/win/resource.h>

APP_ICON ICON "clip_icon.ico"
1 VERSIONINFO
FILEVERSION 3,5,0,0
PRODUCTVERSION 3,5,0,0
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904E4"
BEGIN
VALUE "FileDescription", "Clipboard Share"
VALUE "FileVersion", "3.5.0"
VALUE "InternalName", "clip_share"
VALUE "LegalCopyright", "H. Thevindu J. Wijesekera"
VALUE "OriginalFilename", "clip_share.exe"
VALUE "ProductName", "ClipShare"
VALUE "ProductVersion", "3.5.0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x809, 1252
END
END
File renamed without changes.
6 changes: 3 additions & 3 deletions winres/resource.h → res/win/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef WINRES_RESOURCE_H_
#define WINRES_RESOURCE_H_
#ifndef RES_WIN_RESOURCE_H_
#define RES_WIN_RESOURCE_H_
#ifdef _WIN32

#define APP_ICON 912

#endif
#endif // WINRES_RESOURCE_H_
#endif // RES_WIN_RESOURCE_H_
7 changes: 6 additions & 1 deletion utils/mac_menu.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#import <utils/kill_others.h>
#include <utils/mac_menu.h>

extern char res_mac_icon_png[];
extern unsigned int res_mac_icon_png_len;

const char *global_prog_name;

@implementation NSApplication (KillInstances) // NOLINT
Expand All @@ -37,7 +40,9 @@ void show_menu_icon(void) {
@autoreleasepool {
NSApplication *app = [NSApplication sharedApplication];
NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
NSImage *iconImage = [NSImage imageNamed:@"icon.png"];

NSData *iconData = [NSData dataWithBytesNoCopy:res_mac_icon_png length:res_mac_icon_png_len];
NSImage *iconImage = [[NSImage alloc] initWithData:iconData];
if (!iconImage) return;
[statusItem.button setImage:iconImage];

Expand Down

0 comments on commit 53337e0

Please sign in to comment.