forked from jimeh/build-emacs-for-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-emacs-for-macos
executable file
·1438 lines (1154 loc) · 39 KB
/
build-emacs-for-macos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'English'
require 'date'
require 'erb'
require 'etc'
require 'fileutils'
require 'json'
require 'net/http'
require 'optparse'
require 'pathname'
require 'time'
require 'tmpdir'
require 'uri'
require 'yaml'
class Error < StandardError; end
module Output
def info(msg, newline: true)
out "INFO: #{msg}", newline: newline
end
def out(msg, newline: true)
if newline
warn "==> #{msg}"
else
$stderr.print "==> #{msg}"
end
end
def err(msg = nil)
raise Error, msg
end
end
module System
include Output
def run_cmd(*args)
out("CMD: #{args.join(' ')}")
cmd(*args)
end
def cmd(*args)
system(*args) || err("Exit code: #{$CHILD_STATUS.exitstatus}")
end
end
class OS
def self.version
@version ||= OSVersion.new
end
def self.arch
@arch ||= `uname -m`.strip
end
end
class OSVersion
def initialize
@version = `sw_vers -productVersion`.match(
/(?<major>\d+)(?:\.(?<minor>\d+)(:?\.(?<patch>\d+))?)?/
)
end
def to_s
@to_s ||= major >= 11 ? major.to_s : "#{major}.#{minor}"
end
def major
@major ||= @version[:major]&.to_i
end
def minor
@minor ||= @version[:minor]&.to_i
end
def patch
@patch ||= @version[:patch]&.to_i
end
end
class Build
include Output
include System
EMACS_MIRROR_REPO = 'emacs-ng/emacs-ng'
DOWNLOAD_URL = 'https://github.com/emacs-ng/emacs-ng/tarball/%s'
attr_reader :root_dir
attr_reader :source_dir
attr_reader :ref
attr_reader :options
attr_reader :gcc_info
def initialize(root_dir, ref = nil, options = {})
@root_dir = root_dir
@ref = ref || 'master'
@options = options
@gcc_info = GccInfo.new
end
def build
load_plan(options[:plan]) if options[:plan]
unless meta[:sha] && meta[:date]
err 'Failed to get commit info from GitHub.'
end
tarball = download_tarball(meta[:sha])
@source_dir = extract_tarball(tarball, patches(options))
autogen
detect_native_comp if options[:native_comp].nil?
app = compile_source(@source_dir)
build_dir, app = create_build_dir(app)
handle_native_lisp(app)
CLIHelperEmbedder.new(app).embed
CSourcesEmbedder.new(app, @source_dir).embed
LibEmbedder.new(app, brew_dir, extra_libs, options[:relink_eln]).embed
GccLibEmbedder.new(app, gcc_info).embed if options[:native_comp]
archive_build(build_dir) if options[:archive]
end
private
def load_plan(filename)
plan = YAML.safe_load(File.read(filename), [:Time])
@ref = plan.dig('source', 'ref')
@meta = {
sha: plan.dig('source', 'commit', 'sha'),
ref: @ref,
date: plan.dig('source', 'commit', 'date')
}
if plan.dig('output', 'directory')
@output_dir = plan.dig('output', 'directory')
end
if plan.dig('output', 'archive')
@archive_filename = plan.dig('output', 'archive')
end
@build_name = plan.dig('build', 'name') if plan.dig('build', 'name')
end
def tarballs_dir
@tarballs_dir ||= File.join(root_dir, 'tarballs')
end
def sources_dir
@sources_dir ||= File.join(root_dir, 'sources')
end
def output_dir
@output_dir ||= (options[:output] || File.join(root_dir, 'builds'))
end
def brew_dir
@brew_dir ||= `brew --prefix`.chomp
end
def extra_libs
return @extra_libs if @extra_libs
libs = [
File.join(brew_dir, 'opt/expat/lib/libexpat.1.dylib'),
File.join(brew_dir, 'opt/libiconv/lib/libiconv.2.dylib'),
File.join(brew_dir, 'opt/zlib/lib/libz.1.dylib')
]
if options[:native_comp]
libgcc_s = File.join(
brew_dir, 'lib', 'gcc', gcc_info.major_version, 'libgcc_s.1.dylib'
)
libs << libgcc_s if File.exist?(libgcc_s)
end
@extra_libs = libs
end
def download_tarball(sha)
FileUtils.mkdir_p(tarballs_dir)
url = (DOWNLOAD_URL % sha)
filename = "emacs-ng-emacs-ng-#{sha[0..6]}.tgz"
target = File.join(tarballs_dir, filename)
if File.exist?(target)
info "#{filename} already exists locally, attempting to use."
return target
end
info 'Downloading tarball from GitHub. This could take a while, ' \
'please be patient.'
args = ['curl', '-L', url, '-o', target]
log_args = args.clone
if options[:github_auth] && ENV['GITHUB_TOKEN']
args = [args[0]] +
['-H', "Authorization: Token #{ENV['GITHUB_TOKEN']}"] + args[1..-1]
log_args = [log_args[0]] +
['-H', '"Authorization: Token $GITHUB_TOKEN"'] +
log_args[1..-1]
end
out "CMD: #{log_args.join(' ')}"
cmd(*args)
target
end
def extract_tarball(filename, patches = [])
FileUtils.mkdir_p(sources_dir)
dirname = File.basename(filename).gsub(/\.\w+$/, '')
target = File.join(sources_dir, dirname)
if File.exist?(target)
info "#{dirname} source tree exists, attempting to use."
return target
end
info 'Extracting tarball...'
result = run_cmd('tar', '-xzf', filename, '-C', sources_dir)
err 'Tarball extraction failed.' unless result
patches.each { |patch| apply_patch(patch, target) }
target
end
def configure_help
return @configure_help if @configure_help
FileUtils.cd(source_dir) { @configure_help = `./configure --help` }
@configure_help
end
def supports_xwidgets?
@supports_xwidgets ||= !!configure_help.match(/\s+--with-xwidgets\s+/)
end
def supports_tree_sitter?
@supports_tree_sitter ||= !!configure_help.match(
/\s+--with-tree-sitter(\s|=).+/
)
end
def supports_native_comp?
@supports_native_comp ||= !native_comp_configure_flag.nil?
end
def native_comp_configure_match
@native_comp_configure_match ||= configure_help.match(
/\s+?(--with-native(?:comp|-compilation))(.+)?\s+?/
)
end
def native_comp_configure_flag
return @native_comp_configure_flag if @native_comp_configure_flag
return unless native_comp_configure_match&.[](1)
@native_comp_configure_flag = [
native_comp_configure_match[1],
native_comp_configure_flag_arg
].compact.join('=')
end
def native_comp_configure_flag_arg
return @native_comp_configure_flag_arg if @native_comp_configure_flag_arg
return if native_comp_configure_match&.[](2) != '[=TYPE]'
@native_comp_configure_flag_arg = \
(options[:native_full_aot] ? 'aot' : 'yes')
end
def detect_native_comp
info 'Detecting native-comp support: ', newline: false
options[:native_comp] = supports_native_comp?
puts options[:native_comp] ? 'Supported' : 'Not supported'
end
def verify_native_comp
return if supports_native_comp?
err 'This emacs source tree does not support native-comp'
end
def autogen
FileUtils.cd(source_dir) do
if File.exist?('autogen/copy_autogen')
run_cmd 'autogen/copy_autogen'
elsif File.exist?('autogen.sh')
run_cmd './autogen.sh'
end
end
end
def compile_source(source)
target = File.join(source, 'nextstep')
emacs_app = File.join(target, 'Emacs.app')
if File.exist?(emacs_app)
info 'Emacs.app already exists in ' \
"\"#{target.gsub(root_dir + '/', '')}\", attempting to use."
return emacs_app
end
info 'Compiling from source. This will take a while...'
FileUtils.cd(source) do
if options[:native_comp]
info 'Compiling with native-comp enabled'
verify_native_comp
gcc_info.verify_libgccjit
ENV['CFLAGS'] = [
"-I#{File.join(gcc_info.root_dir, 'include')}",
"-I#{File.join(gcc_info.libgccjit_root_dir, 'include')}",
'-O2',
(options[:native_march] ? '-march=native' : nil),
ENV['CFLAGS']
].compact.join(' ')
ENV['LDFLAGS'] = [
"-L#{gcc_info.lib_dir}",
"-L#{gcc_info.darwin_lib_dir}",
"-L#{gcc_info.libgccjit_lib_dir}",
"-I#{File.join(gcc_info.root_dir, 'include')}",
"-I#{File.join(gcc_info.libgccjit_root_dir, 'include')}",
# Ensure library re-linking and code signing will work after building.
'-Wl,-headerpad_max_install_names',
ENV['LDFLAGS']
].compact.join(' ')
ENV['LIBRARY_PATH'] = [
gcc_info.lib_dir,
gcc_info.darwin_lib_dir,
gcc_info.libgccjit_lib_dir,
ENV['LIBRARY_PATH']
].compact.join(':')
end
ENV['CC'] = 'clang'
ENV['PKG_CONFIG_PATH'] = [
File.join(brew_dir, 'lib/pkgconfig'),
File.join(brew_dir, 'share/pkgconfig'),
File.join(brew_dir, 'opt/expat/lib/pkgconfig'),
File.join(brew_dir, 'opt/libxml2/lib/pkgconfig'),
File.join(brew_dir, 'opt/ncurses/lib/pkgconfig'),
File.join(brew_dir, 'opt/zlib/lib/pkgconfig'),
File.join(brew_dir, 'Homebrew/Library/Homebrew/os/mac/pkgconfig',
OS.version.to_s),
ENV['PKG_CONFIG_PATH']
].compact.join(':')
ENV['PATH'] = [
File.join(brew_dir, 'opt/make/libexec/gnubin'),
File.join(brew_dir, 'opt/coreutils/libexec/gnubin'),
File.join(brew_dir, 'opt/gnu-sed/libexec/gnubin'),
File.join(brew_dir, 'bin'),
File.join(brew_dir, 'opt/texinfo/bin'),
ENV['PATH']
].compact.join(':')
ENV['LIBRARY_PATH'] = [
ENV['LIBRARY_PATH'],
'/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib'
].compact.join(':')
configure_flags = [
'--with-modules',
'--enable-locallisppath=' \
'/Library/Application Support/Emacs/${version}/site-lisp:' \
'/Library/Application Support/Emacs/site-lisp:' \
'/usr/local/share/emacs/site-lisp'
]
if options[:webrender]
configure_flags << '--with-webrender'
if !options[:ns]
configure_flags << '--with-winit'
end
end
if !options[:ns]
configure_flags << '--without-ns'
end
if options[:xwidgets] && supports_xwidgets?
configure_flags << '--with-xwidgets'
end
if options[:tree_sitter] && supports_tree_sitter?
configure_flags << '--with-tree-sitter'
end
configure_flags << native_comp_configure_flag if options[:native_comp]
configure_flags << '--without-rsvg' if options[:rsvg] == false
configure_flags << '--without-dbus' if options[:dbus] == false
run_cmd './configure', *configure_flags.compact
# Disable aligned_alloc on Mojave and below. See issue:
# https://github.com/daviderestivo/homebrew-emacs-head/issues/15
if OS.version.major <= 10 && OS.version.minor <= 14
info 'Force disabling of aligned_alloc on macOS Mojave (10.14.x) ' \
'and earlier'
disable_alligned_alloc
end
make_flags = []
make_flags += ['-j', options[:parallel].to_s] if options[:parallel]
if options[:native_comp]
make_flags << "BYTE_COMPILE_EXTRA_FLAGS=--eval '(setq comp-speed 2)'"
if options[:native_full_aot]
info 'Using native compile full AOT'
# We do not need to supply the full AOT make arg if
# --with-native-compilation=aot configure flag is supported.
unless native_comp_configure_flag_arg
make_flags << 'NATIVE_FULL_AOT=1'
end
ENV.delete('NATIVE_FAST_BOOT')
else
ENV.delete('NATIVE_FULL_AOT')
ENV['NATIVE_FAST_BOOT'] = '1'
end
end
run_cmd 'make', *make_flags.compact
run_cmd 'make', 'install'
end
err 'Build failed.' unless File.exist?(emacs_app)
emacs_app
end
def create_build_dir(app)
app_name = File.basename(app)
target_dir = File.join(output_dir, build_name)
if File.exist?(target_dir)
err "Output directory #{target_dir} already exists, " \
'please delete it and try again'
end
info "Copying \"#{app_name}\" to: #{target_dir}"
FileUtils.mkdir_p(target_dir)
cmd('cp', '-a', app, target_dir)
options[:dist_include]&.each do |filename|
src = File.join(source_dir, filename)
if File.exist?(src)
info "Copying \"#{filename}\" to: #{target_dir}"
cmd('cp', '-pRL', src, target_dir)
else
info "Warning: #{filename} does not exist in #{source_dir}"
end
end
[target_dir, File.join(target_dir, File.basename(app))]
end
def handle_native_lisp(app)
return unless options[:native_comp]
contents_dir = File.join(app, 'Contents')
FileUtils.cd(contents_dir) do
source = Dir['MacOS/libexec/emacs/**/eln-cache',
'MacOS/lib/emacs/**/native-lisp'].first
# Skip creation of symlinks if *.eln files are not located in a location
# known to be used by builds which need symlinks and other tweaks.
return if source.nil?
info 'Creating symlinks within Emacs.app needed for native-comp'
if !File.exist?('lisp') && File.exist?('Resources/lisp')
run_cmd('ln', '-s', 'Resources/lisp', 'lisp')
end
# Check for folder name containing two dots (.), as this causes Apple's
# codesign CLI tool to fail signing the Emacs.app bundle, complaining with
# q "bundle format unrecognized" error.
#
# The workaround for now is to rename the folder replacing the dots with
# hyphens (-), and create the native-lisp symlink pointing to the new
# location.
eln_dir = File.dirname(Dir[File.join(source, '**', '*.eln')].first)
if eln_dir.match(%r{/.+\..+\..+/})
base = File.basename(eln_dir)
parent = File.dirname(eln_dir)
until ['.', '/', contents_dir].include?(parent)
if base.match(/\..+\./)
old_name = File.join(parent, base)
new_name = File.join(parent, base.gsub(/\.(.+)\./, '-\\1-'))
info "Renaming: #{old_name} --> #{new_name}"
cmd('mv', old_name, new_name)
end
base = File.basename(parent)
parent = File.dirname(parent)
end
eln_parts = eln_dir.match(
%r{/(\d+\.\d+\.\d+)/native-lisp/(\d+\.\d+\.\d+-\w+)(?:/.+)?$}i
)
if eln_parts
patch_dump_native_lisp_paths(app, eln_parts[1], eln_parts[2])
end
# Find native-lisp directory again after it has been renamed.
source = Dir['MacOS/libexec/emacs/**/eln-cache',
'MacOS/lib/emacs/**/native-lisp'].first
if source.nil?
err 'Failed to find native-lisp cache directory for symlink creation.'
end
end
target = File.basename(source)
run_cmd('ln', '-s', source, target) unless File.exist?(target)
end
end
def patch_dump_native_lisp_paths(app, emacs_version, eln_version)
sanitized_emacs_version = emacs_version.gsub('.', '-')
sanitized_eln_version = eln_version.gsub('.', '-')
contents_dir = File.join(app, 'Contents')
FileUtils.cd(contents_dir) do
filename = Dir['MacOS/Emacs.pdmp', 'MacOS/libexec/Emacs.pdmp'].first
err "no Emacs.pdmp file found in #{app}" unless filename
info 'patching Emacs.pdmp to point at new native-lisp paths'
content = File.read(filename, mode: 'rb').gsub(
"lib/emacs/#{emacs_version}/native-lisp/#{eln_version}/",
"lib/emacs/#{sanitized_emacs_version}/" \
"native-lisp/#{sanitized_eln_version}/"
).gsub(
"../native-lisp/#{eln_version}/",
"../native-lisp/#{sanitized_eln_version}/"
)
File.open(filename, 'w') { |f| f.write(content) }
end
end
def build_name
return @build_name if @build_name
return @build_name = options[:build_name] if options[:build_name]
metadata = [
meta[:date]&.strftime('%Y-%m-%d'),
meta[:sha][0..6],
meta[:ref],
"macOS-#{OS.version}",
OS.arch
].compact.map { |v| v.gsub(/[^\w_-]+/, '-') }
@build_name = "Emacs.#{metadata.join('.')}"
end
def archive_filename
@archive_filename ||= File.join(output_dir, "#{build_name}.tbz")
end
def archive_build(build_dir)
filename = File.basename(archive_filename)
target_dir = File.dirname(archive_filename)
FileUtils.mkdir_p(target_dir)
build = File.basename(build_dir)
parent_dir = File.dirname(build_dir)
if !File.exist?(archive_filename)
info "Creating #{filename} archive in \"#{target_dir}\"..."
FileUtils.cd(parent_dir) do
cmd('tar', '-cjf', archive_filename, build)
if options[:archive_keep] == false
info "Removing \"#{build}\" directory from #{parent_dir}"
FileUtils.rm_rf(build_dir)
end
end
else
info "#{filename} archive exists in " \
"#{target_dir}, skipping archving."
end
end
def disable_alligned_alloc
filename = 'src/config.h'
content = File.read(filename)
.gsub('#define HAVE_ALIGNED_ALLOC 1',
'#undef HAVE_ALIGNED_ALLOC')
.gsub('#define HAVE_DECL_ALIGNED_ALLOC 1',
'#undef HAVE_DECL_ALIGNED_ALLOC')
.gsub('#define HAVE_ALLOCA 1',
'#undef HAVE_ALLOCA')
.gsub('#define HAVE_ALLOCA_H 1',
'#undef HAVE_ALLOCA_H')
File.open(filename, 'w') { |f| f.write(content) }
end
def meta
return @meta if @meta
ref_sha = options[:git_sha] || ref
info "Fetching info for git ref: #{ref_sha}"
commit_json = github_api_get(
"/repos/#{EMACS_MIRROR_REPO}/commits/#{ref_sha}"
)
err "Failed to get commit info about: #{ref_sha}" if commit_json.nil?
commit = JSON.parse(commit_json)
meta = {
sha: commit['sha'],
date: Time.parse(commit['commit']['committer']['date'])
}
meta[:ref] = ref if ref && ref[0..6] != meta[:sha][0..6]
@meta = meta
end
def github_api_get(uri)
uri = URI.join('https://api.github.com/', uri)
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true if uri.scheme == 'https'
request = Net::HTTP::Get.new(uri)
if options[:github_auth] && ENV['GITHUB_TOKEN']
request['Authorization'] = "Token #{ENV['GITHUB_TOKEN']}"
end
response = http.request(request)
return unless response.code == '200'
response.body
end
def effective_version
@effective_version ||= begin
case ref
when /^emacs-26.*/
'emacs-26'
when /^emacs-27.*/
'emacs-27'
when /^emacs-28.*/
'emacs-28'
when /^emacs-29.*/
'emacs-29'
else
'emacs-30'
end
end
end
def patches(opts = {})
p = []
if %w[emacs-26 emacs-27 emacs-28 emacs-29 emacs-30].include?(effective_version)
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/fix-window-role.patch"
}
end
if %w[emacs-27 emacs-28 emacs-29 emacs-30].include?(effective_version)
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/system-appearance.patch"
}
if options[:no_titlebar]
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/no-titlebar.patch"
}
end
if options[:no_frame_refocus]
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/no-frame-refocus-cocoa.patch"
}
end
end
if %w[emacs-29 emacs-30].include?(effective_version)
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/round-undecorated-frame.patch"
}
if options[:poll]
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/poll.patch"
}
end
end
if effective_version == 'emacs-28'
p << {
replace: [
'configure.ac',
'grep libgccjit.so\$',
'grep -E \'libgccjit\.(so|dylib)$\''
],
allow_failure: true
}
end
if %w[emacs-28 emacs-29].include?(effective_version)
p << {
replace: [
'configure.ac',
'grep -E \'libgccjit\.(so|dylib)$\'',
'grep -E \'libgccjit\.(so|dylib)$\' | tail -1'
],
allow_failure: true
}
end
if effective_version == 'emacs-27'
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/ligatures-freeze-fix.patch"
}
if opts[:xwidgets]
p << {
url: 'https://github.com/d12frosted/homebrew-emacs-plus/raw/master/' \
"patches/#{effective_version}/xwidgets_webkit_in_cocoa.patch"
}
end
end
p
end
def apply_patch(patch, target)
err "\"#{target}\" does not exist." unless File.exist?(target)
if patch[:file]
info 'Applying patch...'
FileUtils.cd(target) { run_cmd('patch', '-f', '-p1', '-i', patch[:file]) }
elsif patch[:url]
patch_dir = "#{target}/macos_patches"
run_cmd('mkdir', '-p', patch_dir)
patch_file = File.join(patch_dir, 'patch-{num}.diff')
num = 1
while File.exist?(patch_file.gsub('{num}', num.to_s.rjust(3, '0')))
num += 1
end
patch_file = patch_file.gsub('{num}', num.to_s.rjust(3, '0'))
info "Downloading patch: #{patch[:url]}"
run_cmd('curl', '-L#', patch[:url], '-o', patch_file)
real_patch_url = detect_github_symlink_patch(patch[:url], patch_file)
if real_patch_url
FileUtils.rm(patch_file)
apply_patch({ url: real_patch_url }, target)
else
apply_patch({ file: patch_file }, target)
end
elsif patch[:replace]
err 'Patch replace input error' unless patch[:replace].size == 3
file, before, after = patch[:replace]
info "Applying patch to #{file}..."
filepath = File.join(target, file)
unless File.exist?(filepath)
if patch[:allow_failure]
info "File #{filepath} does not exist, skipping patch."
return
end
err "\"#{file}\" does not exist in #{target}"
end
f = File.open(filepath, 'rb')
s = f.read
sub = s.gsub!(before, after)
if sub.nil?
if patch[:allow_failure]
info 'Patch did not apply, skipping.'
return
end
err "Replacement failed in #{file}"
end
f.reopen(filepath, 'wb').write(s)
f.close
info "#{file} patched."
end
end
# When downloading raw files from GitHub, if the target file is a symlink, it
# will return the actual target path of the symlink instead of the content of
# the target file. Hence we have to check if the patch file we have downloaded
# contains one and only one line, and if so, assume it's a symlink.
def detect_github_symlink_patch(original_url, patch_file)
lines = []
# read first two lines
File.open(patch_file) do |f|
lines << f.gets
lines << f.gets
end
# if the file contains more than one line of text, it's not a symlink.
return unless lines[1].nil?
symlink_target = lines[0].strip
# Assume patch file content is something along the lines of
# "../emacs-28/fix-window-role.patch", hence we resolve it relative to the
# original url.
info "patch is symlink to #{symlink_target}"
URI.join(original_url, symlink_target).to_s
end
end
class AbstractEmbedder
include Output
include System
attr_reader :app
def initialize(app)
err "#{app} does not exist" unless File.exist?(app)
@app = app
end
def invocation_dir
@invocation_dir ||= File.join(app, 'Contents', 'MacOS')
end
def bin
@bin ||= File.join(invocation_dir, 'Emacs')
end
def bin_dir
@bin_dir ||= File.join(invocation_dir, 'bin')
end
def lib_dir
@lib_dir ||= frameworks_dir
end
def frameworks_dir
@frameworks_dir ||= File.join(app, 'Contents', 'Frameworks')
end
def resources_dir
@resources_dir ||= File.join(app, 'Contents', 'Resources')
end
end
class CLIHelperEmbedder < AbstractEmbedder
def embed
source = File.join(__dir__, 'helper', 'emacs-cli.bash')
target = File.join(bin_dir, 'emacs')
dir = File.dirname(target)
info 'Adding "emacs" CLI helper to Emacs.app'
FileUtils.mkdir_p(dir)
run_cmd('cp', '-pRL', source, target)
run_cmd('chmod', '+w', target)
end
end
class CSourcesEmbedder < AbstractEmbedder
PATH_PATCH = <<~ELISP
;; Allow Emacs to find bundled C sources.
(setq source-directory
(expand-file-name ".." (file-name-directory load-file-name)))
ELISP
attr_reader :source_dir
def initialize(app, source_dir)
super(app)
@source_dir = source_dir
end
def embed
info 'Embedding C source files into Emacs.app for documentation purposes'
src_dir = File.join(source_dir, 'src')
Dir[File.join(src_dir, '**', '*.{c,h}')].each do |f|
rel = f[src_dir.size + 1..-1]
target = File.join(resources_dir, 'src', rel)
FileUtils.mkdir_p(File.dirname(target))
cmd('cp', '-pRL', f, target)
end
return if File.exist?(site_start_el_file) &&
File.read(site_start_el_file).include?(PATH_PATCH)
File.open(site_start_el_file, 'a') do |f|
f.puts("\n#{PATH_PATCH}")
end
end
private
def site_start_el_file
@site_start_el_file ||= File.join(resources_dir, 'lisp', 'site-start.el')
end
end
class LibEmbedder < AbstractEmbedder
attr_reader :lib_source
attr_reader :extra_libs
attr_reader :relink_eln_files
def initialize(app, lib_source, extra_libs = [], relink_eln_files = true)
super(app)
@lib_source = lib_source
@extra_libs = extra_libs
@relink_eln_files = relink_eln_files
end
def embed
info 'Embedding libraries into Emacs.app'
binary = "#{bin}-bin" if File.exist?("#{bin}-bin")
binary ||= bin
FileUtils.cd(File.dirname(app)) do
rel_path = Pathname.new(lib_dir).relative_path_from(
Pathname.new(File.dirname(binary))
).to_s
rpath = File.join('@executable_path', rel_path)
set_rpath(binary, rpath)
copy_libs(binary)
copy_extra_libs(extra_libs, binary) if extra_libs.any?
if relink_eln_files && eln_files.any?
info "Embedding libraries for #{eln_files.size} *.eln files " \
'within Emacs.app'
eln_files.each { |f| copy_libs(f) }
end
end
end
private
def eln_files
@eln_files ||= Dir[File.join(app, 'Contents', '**', '*.eln')]
end
def set_rpath(exe, rpath)
return if rpath.nil? || rpath == ''
rpaths = `otool -l "#{exe}" | grep -A 2 'cmd LC_RPATH' | grep 'path'`
return if rpaths.include?(rpath)
while_writable(exe) do
cmd('install_name_tool', '-add_rpath', rpath, exe)
end
end
def copy_libs(exe)
exe_file = File.basename(exe)