-
Notifications
You must be signed in to change notification settings - Fork 310
/
install.R
3038 lines (2843 loc) · 128 KB
/
install.R
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
# File src/library/tools/R/install.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2021 The R Core Team
#
# NB: also copyright dates in Usages.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
#### R based engine for R CMD INSTALL SHLIB Rprof
####
##' @param args
## R developers can use this to debug the function by running it
## directly as tools:::.install_packages(args), where the args should
## be what commandArgs(TRUE) would return, that is a character vector
## of (space-delimited) terms that would be passed to R CMD INSTALL. E.g.
##
if(FALSE) {
tools:::.install_packages(c("--preclean", "--no-multiarch",
"tree"))
## or
status <- tryCatch(
tools:::.install_packages(c("--no-clean-on-error", "--no-multiarch",
"tree"), no.q = TRUE)
, error = function(e) as.numeric(sub(".* exit status *", "",
conditionMessage(e))))
## or
debugonce(tools:::.install_packages)
tools:::.install_packages(c("-c", "--debug", "--no-clean-on-error", "--no-multiarch",
"tree"))
## and then (after about 40 x [Enter]) when do_install is defined:
debug(do_install) ## and 'c'
}
##' @return ...
.install_packages <- function(args = NULL, no.q = interactive(), warnOption = 1)
{
## calls system() on Windows for
## sh (configure.win/cleanup.win) make zip
## global variables
curPkg <- character() # list of packages in current pkg
lockdir <- ""
is_first_package <- TRUE
stars <- "*"
user.tmpdir <- Sys.getenv("PKG_BUILD_DIR")
keep.tmpdir <- nzchar(user.tmpdir)
## Need these here in case of an early error, e.g. missing etc/Makeconf
tmpdir <- ""
clean_on_error <- TRUE
R_runR_deps_only <- function(cmd, deps_only_env, multiarch = FALSE, ...) {
deps_only <-
config_val_to_logical(Sys.getenv("_R_CHECK_INSTALL_DEPENDS_",
"FALSE"))
env <- if (deps_only) deps_only_env
else ""
## needed for some packages (AnnotationDbi) that install other
## packages during their tests (otherwise system profile fails
## because it cannot find the tests startup file)
env <- paste(env, "R_TESTS=")
opts <- "--no-save --no-restore --no-echo"
if (deps_only) {
opts <- paste(opts, "--no-init-file --no-site-file")
if (!multiarch)
## do not use --no-environ with multiarch, because Renviron
## may include architecture-specific settings that may differ
## from settings of the host process architecture
opts <- paste(opts, "--no-environ")
}
R_runR(cmd = cmd, Ropts = opts, env = env, ...)
}
do_exit <-
if(no.q)
function(status) stop(".install_packages() exit status ", status)
else
function(status) q("no", status = status, runLast = FALSE)
do_exit_on_error <- function(status = 1L)
{
## If we are not yet processing a package, we will not have
## set curPkg
if(clean_on_error && length(curPkg)) {
pkgdir <- file.path(lib, curPkg)
if (nzchar(pkgdir) && dir.exists(pkgdir) &&
is_subdir(pkgdir, lib)) {
starsmsg(stars, "removing ", sQuote(pkgdir))
unlink(pkgdir, recursive = TRUE)
}
if (nzchar(lockdir) &&
dir.exists(lp <- file.path(lockdir, curPkg)) &&
is_subdir(lp, lockdir)) {
starsmsg(stars, "restoring previous ", sQuote(pkgdir))
if (WINDOWS) {
file.copy(lp, dirname(pkgdir), recursive = TRUE,
copy.date = TRUE)
unlink(lp, recursive = TRUE)
} else {
## some shells require that they be run in a known dir
setwd(startdir)
if(system(paste("mv -f", shQuote(lp), shQuote(pkgdir))))
message(" restoration failed\n")
}
}
}
do_cleanup()
do_exit(status=status)
}
do_cleanup <- function()
{
if(!keep.tmpdir && nzchar(tmpdir)) do_cleanup_tmpdir()
if (!is_first_package) {
## Only need to do this in case we successfully installed
## at least one package
if (lib == .Library && "html" %in% build_help_types)
utils::make.packages.html(.Library, docdir = R.home("doc"))
}
if (nzchar(lockdir)) unlink(lockdir, recursive = TRUE)
}
do_cleanup_tmpdir <- function()
{
## Solaris will not remove any directory in the current path
setwd(startdir)
if (!keep.tmpdir && dir.exists(tmpdir)) unlink(tmpdir, recursive=TRUE)
}
# This produces a (by default single) quoted string for use in a
# command sent to another R process.
quote_path <- function(path, quote = "'") {
path <- gsub("\\", "\\\\", path, fixed = TRUE)
path <- gsub(quote, paste0("\\", quote), path, fixed = TRUE)
paste0(quote, path, quote)
}
# Escape backslashes in a replacement string for gsub etc.
# To be used when the replacement is a path name which may include
# backslashes, e.g. with UNC paths on Windows.
quote_replacement <- function(r)
paste0(gsub("\\", "\\\\", r, fixed=TRUE))
on.exit(do_exit_on_error())
WINDOWS <- .Platform$OS.type == "windows"
cross <- Sys.getenv("R_CROSS_BUILD")
have_cross <- nzchar(cross)
if(have_cross && !cross %in% c("i386", "x64"))
stop("invalid value ", sQuote(cross), " for R_CROSS_BUILD")
if (have_cross) {
WINDOWS <- TRUE
Sys.setenv(R_OSTYPE = "windows")
}
if (WINDOWS) MAKE <- "make"
else MAKE <- Sys.getenv("MAKE") # FIXME shQuote, default?
rarch <- Sys.getenv("R_ARCH") # unix only
if (WINDOWS && nzchar(.Platform$r_arch))
rarch <- paste0("/", .Platform$r_arch)
cross <- Sys.getenv("R_CROSS_BUILD")
if(have_cross && !cross %in% c("i386", "x64"))
stop("invalid value ", sQuote(cross), " for R_CROSS_BUILD")
test_archs <- rarch
if (have_cross) {
WINDOWS <- TRUE
r_arch <- paste0("/", cross)
test_archs <- c()
}
SHLIB_EXT <- if (WINDOWS) ".dll" else {
## can we do better?
mconf <- file.path(R.home(), paste0("etc", rarch), "Makeconf")
## PCRE needed for Debian arm* platforms
sub(".*= ", "", grep("^SHLIB_EXT", readLines(mconf), value = TRUE,
perl = TRUE))
}
if(getOption("warn") < warnOption) {
op <- options(warn = warnOption)
on.exit(options(op), add = TRUE)
}
invisible(Sys.setlocale("LC_COLLATE", "C")) # discard output
if (WINDOWS) {
rhome <- chartr("\\", "/", R.home())
## These might be needed for configure.win and Make{file,vars}.win
## Some people have *assumed* that R_HOME uses /
Sys.setenv(R_HOME = rhome)
if (nzchar(rarch)) Sys.setenv(R_ARCH = rarch, R_ARCH_BIN = rarch)
}
Usage <- function() {
cat("Usage: R CMD INSTALL [options] pkgs",
"",
"Install the add-on packages specified by pkgs. The elements of pkgs can",
"be relative or absolute paths to directories with the package",
"sources, or to gzipped package 'tar' archives. The library tree",
"to install to can be specified via '--library'. By default, packages are",
"installed in the library tree rooted at the first directory in",
".libPaths() for an R session run in the current environment.",
"",
"Options:",
" -h, --help print short help message and exit",
" -v, --version print INSTALL version info and exit",
" -c, --clean remove files created during installation",
" --preclean remove files created during a previous run",
" -d, --debug turn on debugging messages",
if(WINDOWS) " and build a debug DLL",
" -l, --library=LIB install packages to library tree LIB",
" --no-configure do not use the package's configure script",
" --no-docs do not install HTML, LaTeX or examples help",
" --html build HTML help",
" --no-html do not build HTML help",
" --latex install LaTeX help",
" --example install R code for help examples",
" --fake do minimal install for testing purposes",
" --no-lock install on top of any existing installation",
" without using a lock directory",
" --lock use a per-library lock directory (default)",
" --pkglock use a per-package lock directory",
" (default for a single package)",
" --build build binaries of the installed package(s)",
" --install-tests install package-specific tests (if any)",
" --no-R, --no-libs, --no-data, --no-help, --no-demo, --no-exec,",
" --no-inst",
" suppress installation of the specified part of the",
" package for testing or other special purposes",
" --no-multiarch build only the main architecture",
" --libs-only only install the libs directory",
" --data-compress= none, gzip (default), bzip2 or xz compression",
" to be used for lazy-loading of data",
" --resave-data re-save data files as compactly as possible",
" --compact-docs re-compress PDF files under inst/doc",
" --with-keep.source",
" --without-keep.source",
" use (or not) 'keep.source' for R code",
" --with-keep.parse.data",
" --without-keep.parse.data",
" use (or not) 'keep.parse.data' for R code",
" --byte-compile byte-compile R code",
" --no-byte-compile do not byte-compile R code",
" --staged-install install to a temporary directory and then move",
" to the target directory (default)",
" --no-staged-install install directly to the target directory",
" --no-test-load skip test of loading installed package",
" --no-clean-on-error do not remove installed package on error",
" --merge-multiarch multi-arch by merging (from a single tarball only)",
" --use-vanilla do not read any Renviron or Rprofile files",
" --use-LTO use Link-Time Optimization",
" --no-use-LTO do not use Link-Time Optimization",
"\nfor Unix",
" --configure-args=ARGS",
" set arguments for the configure scripts (if any)",
" --configure-vars=VARS",
" set variables for the configure scripts (if any)",
" --strip strip shared object(s)",
" --strip-lib strip static/dynamic libraries under lib/",
" --dsym (macOS only) generate dSYM directory",
" --built-timestamp=STAMP",
" set timestamp for Built: entry in DESCRIPTION",
"\nand on Windows only",
" --force-biarch attempt to build both architectures",
" even if there is a non-empty configure.win",
" --compile-both compile both architectures on 32-bit Windows",
"",
"Which of --html or --no-html is the default depends on the build of R:",
paste0("for this one it is ",
if(static_html) "--html" else "--no-html", "."),
"",
"Report bugs at <https://bugs.R-project.org>.", sep = "\n")
}
## Check whether dir is a subdirectory of parent,
## to protect against malicious package names like ".." below
## Assumes that both directories exist
is_subdir <- function(dir, parent) {
rl <- Sys.readlink(dir) ## symbolic link (on POSIX, not Windows) is ok:
(!is.na(rl) && nzchar(rl)) ||
normalizePath(parent) == normalizePath(file.path(dir, ".."))
}
fullpath <- function(dir)
{
owd <- setwd(dir)
full <- getwd()
setwd(owd)
full
}
## used for LazyData, KeepSource, ByteCompile, Biarch, StagedInstall
parse_description_field <- function(desc, field, default)
str_parse_logic(desc[field], default = default,
otherwise = quote(
errmsg("invalid value of ", field, " field in DESCRIPTION")))
starsmsg <- function(stars, ...)
message(stars, " ", ..., domain = NA)
errmsg <- function(...)
{
message("ERROR: ", ..., domain = NA)
do_exit_on_error()
}
pkgerrmsg <- function(msg, pkg)
errmsg(msg, " for package ", sQuote(pkg))
## 'pkg' is the absolute path to package sources.
do_install <- function(pkg)
{
if (WINDOWS && endsWith(pkg, ".zip")) {
pkg_name <- basename(pkg)
pkg_name <- sub("\\.zip$", "", pkg_name)
pkg_name <- sub("_[0-9.-]+$", "", pkg_name)
reuse_lockdir <- lock && !pkglock
if (pkglock)
lock <- "pkglock"
utils:::unpackPkgZip(pkg, pkg_name, lib, libs_only, lock,
reuse_lockdir = reuse_lockdir)
return()
}
setwd(pkg)
## We checked this exists, but not that it is readable
desc <- tryCatch(read.dcf(fd <- file.path(pkg, "DESCRIPTION")),
error = identity)
if(inherits(desc, "error") || !length(desc))
stop(gettextf("error reading file '%s'", fd),
domain = NA, call. = FALSE)
desc <- desc[1L,]
## Let's see if we have a bundle
if (!is.na(desc["Bundle"])) {
stop("this seems to be a bundle -- and they are defunct")
} else {
pkg_name <- desc["Package"]
if (is.na(pkg_name)) errmsg("no 'Package' field in 'DESCRIPTION'")
curPkg <<- pkg_name
}
instdir <- file.path(lib, pkg_name) # = <library>/<pkg>
Sys.setenv(R_PACKAGE_NAME = pkg_name, R_PACKAGE_DIR = instdir)
status <- .Rtest_package_depends_R_version()
if (status) do_exit_on_error()
dir.create(instdir, recursive = TRUE, showWarnings = FALSE)
if (!dir.exists(instdir)) {
message("ERROR: unable to create ", sQuote(instdir), domain = NA)
do_exit_on_error()
}
if (!is_subdir(instdir, lib)) {
message("ERROR: ", sQuote(pkg_name), " is not a legal package name",
domain = NA)
do_exit_on_error()
}
## Make sure we do not attempt installing to srcdir.
owd <- setwd(instdir)
if (owd == getwd()) pkgerrmsg("cannot install to srcdir", pkg_name)
setwd(owd)
## Figure out whether this is a source or binary package.
is_source_package <- is.na(desc["Built"])
if (is_source_package) {
## Find out if any C++ standard is requested in DESCRIPTION file
sys_requires <- desc["SystemRequirements"]
if (!is.na(sys_requires)) {
sys_requires <- unlist(strsplit(sys_requires, ","))
for (i in cxx_standards) {
pattern <- paste0("^[[:space:]]*C[+][+]",i,"[[:space:]]*$")
if(any(grepl(pattern, sys_requires, ignore.case=TRUE))) {
Sys.setenv("R_PKG_CXX_STD"=i)
on.exit(Sys.unsetenv("R_PKG_CXX_STD"))
break
}
}
}
}
if (!is_first_package) cat("\n")
if (is_source_package)
do_install_source(pkg_name, instdir, pkg, desc)
else
do_install_binary(pkg_name, instdir, desc)
## Add read permission to all, write permission to owner
## If group-write permissions were requested, set them
.Call(C_dirchmod, instdir, group.writable)
is_first_package <<- FALSE
if (tar_up) { # Unix only
starsmsg(stars, "creating tarball")
version <- desc["Version"]
filename <- if (!grepl("darwin", R.version$os)) {
paste0(pkg_name, "_", version, "_R_",
Sys.getenv("R_PLATFORM"), ".tar.gz")
} else {
paste0(pkg_name, "_", version,".tgz")
}
filepath <- file.path(startdir, filename)
owd <- setwd(lib)
res <- utils::tar(filepath, curPkg, compression = "gzip",
compression_level = 9L,
tar = Sys.getenv("R_INSTALL_TAR"))
if (res)
errmsg(sprintf("packaging into %s failed", sQuote(filename)))
message("packaged installation of ",
sQuote(pkg_name), " as ", sQuote(filename),
domain = NA)
setwd(owd)
}
if (zip_up) { # Windows only
starsmsg(stars, "MD5 sums")
.installMD5sums(instdir)
## we could use utils::zip() here.
ZIP <- "zip" # Windows only
version <- desc["Version"]
filename <- paste0(pkg_name, "_", version, ".zip")
filepath <- shQuote(file.path(startdir, filename))
## system(paste("rm -f", filepath))
unlink(filepath)
owd <- setwd(lib)
res <- system(paste(shQuote(ZIP), "-r9Xq", filepath,
paste(curPkg, collapse = " ")))
setwd(owd)
if (res)
message("running 'zip' failed", domain = NA)
else
message("packaged installation of ",
sQuote(pkg_name), " as ", filename, domain = NA)
}
if (Sys.getenv("_R_INSTALL_NO_DONE_") != "yes") {
## message("", domain = NA) # ensure next starts on a new line, for R CMD check
starsmsg(stars, "DONE (", pkg_name, ")")
}
curPkg <<- character()
}
## Unix only
do_install_binary <- function(pkg, instdir, desc)
{
starsmsg(stars, "installing *binary* package ", sQuote(pkg), " ...")
if (file.exists(file.path(instdir, "DESCRIPTION"))) {
if (nzchar(lockdir))
system(paste("mv -f", shQuote(instdir),
shQuote(file.path(lockdir, pkg))))
dir.create(instdir, recursive = TRUE, showWarnings = FALSE)
}
TAR <- Sys.getenv("TAR", 'tar')
res <- system(paste("cp -R .", shQuote(instdir),
"|| (", TAR, "cd - .| (cd", shQuote(instdir), "&&", TAR, "-xf -))"
))
if (res) errmsg("installing binary package failed")
if (tar_up) {
starsmsg(stars, sQuote(pkg),
" was already a binary package and will not be rebuilt")
tar_up <- FALSE
}
}
## to be run from package source directory
run_clean <- function()
{
if (dir.exists("src") && length(dir("src", all.files = TRUE) > 2L)) {
if (WINDOWS) archs <- c("i386", "x64")
else {
wd2 <- setwd(file.path(R.home("bin"), "exec"))
archs <- Sys.glob("*")
setwd(wd2)
}
if(length(archs))
for(arch in archs) {
ss <- paste0("src-", arch)
## it seems fixing permissions is sometimes needed
.Call(C_dirchmod, ss, group.writable)
unlink(ss, recursive = TRUE)
}
owd <- setwd("src")
if (WINDOWS) {
if (file.exists("Makefile.win"))
system(paste(MAKE, "-f Makefile.win clean"))
else
unlink(c("Makedeps",
Sys.glob("*_res.rc"),
Sys.glob("*.[do]")))
# system("rm -f *_res.rc *.o *.d Makedeps")
} else {
if (file.exists("Makefile")) system(paste(MAKE, "clean"))
else ## we will be using SHLIB --preclean
unlink(Sys.glob(paste0("*", SHLIB_EXT)))
}
setwd(owd)
}
if (WINDOWS) {
if (file.exists("cleanup.win")) system("sh ./cleanup.win")
} else if (file_test("-x", "cleanup")) system("./cleanup")
else if (file.exists("cleanup"))
warning("'cleanup' exists but is not executable -- see the 'R Installation and Administration Manual'", call. = FALSE)
}
do_install_source <- function(pkg_name, instdir, pkg_dir, desc)
{
Sys.setenv("R_INSTALL_PKG" = pkg_name)
on.exit(Sys.unsetenv("R_INSTALL_PKG"))
shlib_install <- function(instdir, arch)
{
## install.libs.R allows customization of the libs installation process
if (file.exists("install.libs.R")) {
message("installing via 'install.libs.R' to ", instdir,
domain = NA)
## the following variables are defined to be available,
## and to prevent abuse we don't expose anything else
local.env <- local({ SHLIB_EXT <- SHLIB_EXT
R_PACKAGE_DIR <- instdir
R_PACKAGE_NAME <- pkg_name
R_PACKAGE_SOURCE <- pkg_dir
R_ARCH <- arch
WINDOWS <- WINDOWS
environment()})
parent.env(local.env) <- .GlobalEnv
source("install.libs.R", local = local.env)
return(TRUE)
}
## otherwise proceed with the default which is to just copy *${SHLIB_EXT}
files <- Sys.glob(paste0("*", SHLIB_EXT))
if (length(files)) {
libarch <- if (nzchar(arch)) paste0("libs", arch) else "libs"
dest <- file.path(instdir, libarch)
message('installing to ', dest, domain = NA)
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
file.copy(files, dest, overwrite = TRUE)
if((do_strip || config_val_to_logical(Sys.getenv("_R_SHLIB_STRIP_",
"false"))) &&
nzchar(strip_cmd <- Sys.getenv("R_STRIP_SHARED_LIB"))) {
system(paste(c(strip_cmd,
shQuote(file.path(dest, files))),
collapse = " "))
}
## not clear if this is still necessary, but sh version did so
if (!WINDOWS)
Sys.chmod(file.path(dest, files), dmode)
## macOS does not keep debugging symbols in binaries
## anymore so optionally we can create dSYMs. This is
## important since we will blow away .o files so there
## is no way to create it later.
if (dsym && startsWith(R.version$os, "darwin")) {
starsmsg(stars, gettextf("generating debug symbols (%s)", "dSYM"))
dylib <- Sys.glob(paste0(dest, "/*", SHLIB_EXT))
for (d in dylib) system(paste0("dsymutil ", d))
}
if(config_val_to_logical(Sys.getenv("_R_SHLIB_BUILD_OBJECTS_SYMBOL_TABLES_",
"TRUE"))
&& file_test("-f", "symbols.rds")) {
file.copy("symbols.rds", dest)
}
}
}
## This is only called for Makevars[.win], so assume it
## does create a shlib: not so reliably reported on Windows
## Note though that it may not create pkg_name.dll, and
## graph does not.
run_shlib <- function(pkg_name, srcs, instdir, arch, use_LTO = NA)
{
args <- c(shargs,
if(isTRUE(use_LTO)) "--use-LTO",
if(isFALSE(use_LTO)) "--no-use-LTO",
"-o", paste0(pkg_name, SHLIB_EXT),
srcs)
if (WINDOWS && debug) args <- c(args, "--debug")
if (debug) message("about to run ",
"R CMD SHLIB ", paste(args, collapse = " "),
domain = NA)
if (.shlib_internal(args) == 0L) {
if(WINDOWS && !file.exists("install.libs.R")
&& !length(Sys.glob("*.dll"))) {
message("no DLL was created")
return(TRUE)
}
shlib_install(instdir, arch)
return(FALSE)
} else return(TRUE)
}
## Patch hardcoded paths in shared objects/dynamic libraries
## so that they can be moved to a different directory.
## Not used on WINDOWS.
patch_rpaths <- function()
{
slibs <- list.files(instdir, recursive = TRUE, all.files = TRUE,
full.names = TRUE)
slibs <- grep("(\\.sl$)|(\\.so$)|(\\.dylib$)|(\\.dll$)", slibs,
value = TRUE)
if (!length(slibs)) return()
have_file <- nzchar(Sys.which("file"))
## file reports macOS dylibs as 'dynamically linked shared library'
if (have_file) {
## RcppParallel has .so files containing ASCII text
## (linker script) which make the tools below produce
## a lot of error messages. However, some docker
## installations do not have "file" utility.
## Solaris' "file" does not use 'shared'.
##
## On macOS, a single "dylib" file can have a shared object
## for multiple architectures, so multiple lines with
## "shared"/"dynamically linked"
are_shared <- vapply(slibs,
function(l) any(grepl("(shared|dynamically linked)",
system(paste("file", shQuote(l)), intern = TRUE))),
NA)
slibs <- slibs[are_shared]
if (!length(slibs)) return()
}
starsmsg(stars, "checking absolute paths in shared objects and dynamic libraries")
uname <- system("uname -a", intern = TRUE)
os <- sub(" .*", "", uname)
have_chrpath <- nzchar(Sys.which("chrpath"))
have_patchelf <- nzchar(Sys.which("patchelf"))
have_readelf <- nzchar(Sys.which("readelf"))
have_macos_clt <- identical(os, "Darwin") &&
nzchar(Sys.which("otool")) &&
nzchar(Sys.which("install_name_tool"))
have_solaris_elfedit <- identical(os, "SunOS") &&
nzchar(Sys.which("elfedit"))
hardcoded_paths <- FALSE
failed_fix <- FALSE
# paths below are protected from the shell, but special
# characters/spaces should not be used, because they cannot be
# reproduced correctly in the outputs from external tools
if (have_solaris_elfedit) {
## Solaris only
## changes both rpath and DT_NEEDED paths
for (l in slibs) {
out <- suppressWarnings(
system(paste("elfedit -re dyn:value", shQuote(l)), intern = TRUE))
out <- grep("^[ \t]*\\[[0-9]+\\]", out, value = TRUE)
re <- "^[ \t]*\\[([0-9]+)\\][ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]*(.*)"
paths <- gsub(re, "\\4", out)
idxs <- gsub(re, "\\1", out)
old_paths <- paths
# "\\$ORIGIN/.."
paths <- gsub(instdir, quote_replacement(final_instdir),
paths, fixed = TRUE)
changed <- paths != old_paths
paths <- paths[changed]
old_paths <- old_paths[changed]
idxs <- idxs[changed]
for (i in seq_along(paths)) {
hardcoded_paths <- TRUE
qp <- gsub('([" \\])', "\\\\\\1", paths[i])
qp <- gsub("'", "\\\\'", qp)
cmd <- paste0("elfedit -e \"dyn:value -dynndx -s ",
idxs[i], " ", qp, "\" ", shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd, intern = FALSE))
if (ret == 0)
message("NOTE: fixed path ", sQuote(old_paths[i]))
}
out <- suppressWarnings(
system(paste("elfedit -re dyn:value", shQuote(l)), intern = TRUE))
out <- grep("^[ \t]*\\[", out, value = TRUE)
paths <- gsub(re, "\\4", out)
if (any(grepl(instdir, paths, fixed = TRUE)))
failed_fix <- TRUE
}
} else if (have_macos_clt) {
## macOS only
for (l in slibs) {
## change identification name of the library
out <- suppressWarnings(
system(paste("otool -D", shQuote(l)), intern = TRUE))
out <- out[-1L] # first line is l (includes instdir)
oldid <- out
if (length(oldid) == 1 &&
grepl(instdir, oldid, fixed = TRUE)) {
hardcoded_paths <- TRUE
newid <- gsub(instdir, quote_replacement(final_instdir),
oldid, fixed = TRUE)
cmd <- paste("install_name_tool -id", shQuote(newid),
shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd, intern = FALSE))
if (ret == 0)
## NOTE: install_name does not signal an error in
## some cases
message("NOTE: fixed library identification name ",
sQuote(oldid))
}
## change paths to other libraries
out <- suppressWarnings(
system(paste("otool -L", shQuote(l)), intern = TRUE))
paths <- grep("\\(compatibility", out, value = TRUE)
paths <- gsub("^[ \t]*(.*) \\(compatibility.*", "\\1",
paths)
old_paths <- paths
# "@loader_path/.."
paths <- gsub(instdir, quote_replacement(final_instdir),
paths, fixed = TRUE)
changed <- paths != old_paths
paths <- paths[changed]
old_paths <- old_paths[changed]
for(i in seq_along(paths)) {
hardcoded_paths <- TRUE
cmd <- paste("install_name_tool -change",
shQuote(old_paths[i]), shQuote(paths[i]),
shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd, intern = FALSE))
if (ret == 0)
## NOTE: install_name does not signal an error in
## some cases
message("NOTE: fixed library path ",
sQuote(old_paths[i]))
}
out <- suppressWarnings(
system(paste("otool -L", shQuote(l)), intern = TRUE))
out <- grep("\\(compatibility", out, value = TRUE)
if (any(grepl(instdir, out, fixed = TRUE)))
failed_fix <- TRUE
## change rpath entries
out <- suppressWarnings(
system(paste("otool -l", shQuote(l)), intern = TRUE))
out <- grep("(^[ \t]*cmd )|(^[ \t]*path )", out,
value = TRUE)
rpidx <- grep("cmd LC_RPATH$", out)
if (length(rpidx)) {
paths <- gsub("^[ \t]*path ", "", out[rpidx+1])
paths <- gsub("(.*) \\(offset .*", "\\1", paths)
old_paths <- paths
# "@loader_path/.."
paths <- gsub(instdir, quote_replacement(final_instdir),
paths, fixed = TRUE)
changed <- paths != old_paths
paths <- paths[changed]
old_paths <- old_paths[changed]
for(i in seq_along(paths)) {
hardcoded_paths <- TRUE
cmd <- paste("install_name_tool -rpath",
shQuote(old_paths[i]),
shQuote(paths[i]),
shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd))
if (ret == 0)
message("NOTE: fixed rpath ",
sQuote(old_paths[i]))
}
}
## check no hard-coded paths are left
out <- suppressWarnings(
system(paste("otool -l", shQuote(l)), intern = TRUE))
out <- out[-1L] # first line is l (includes instdir)
if (any(grepl(instdir, out, fixed = TRUE)))
failed_fix <- TRUE
}
} else if (have_patchelf) {
## probably Linux
for(l in slibs) {
# fix rpath
rpath <- suppressWarnings(
system(paste("patchelf --print-rpath", shQuote(l)),
intern = TRUE))
old_rpath <- rpath
# "\\$ORIGIN/.."
rpath <- gsub(instdir, quote_replacement(final_instdir),
rpath, fixed = TRUE)
if (length(rpath) && nzchar(rpath) && old_rpath != rpath) {
hardcoded_paths <- TRUE
cmd <- paste("patchelf", "--set-rpath",
shQuote(rpath), shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd))
if (ret == 0)
message("NOTE: fixed rpath ", sQuote(old_rpath))
rpath <- suppressWarnings(
system(paste("patchelf --print-rpath",
shQuote(l)), intern = TRUE))
if (any(grepl(instdir, rpath, fixed = TRUE)))
failed_fix <- TRUE
}
# fix DT_NEEDED
if (have_readelf) {
out <- suppressWarnings(
system(paste("readelf -d", shQuote(l)), intern = TRUE))
re0 <- "0x.*\\(NEEDED\\).*Shared library:"
out <- grep(re0, out, value = TRUE)
re <- "^[ \t]*0x[0-9]+[ \t]+\\(NEEDED\\)[ \t]+Shared library:[ \t]*\\[(.*)\\]"
paths <- gsub(re, "\\1", out)
old_paths <- paths
# "\\$ORIGIN/.."
paths <- gsub(instdir, quote_replacement(final_instdir),
paths, fixed = TRUE)
changed <- paths != old_paths
paths <- paths[changed]
old_paths <- old_paths[changed]
for(i in seq_along(paths)) {
cmd <- paste("patchelf --replace-needed",
shQuote(old_paths[i]),
shQuote(paths[i]),
shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd))
if (ret == 0)
message("NOTE: fixed library path ",
sQuote(old_paths[i]))
}
out <- suppressWarnings(
system(paste("readelf -d", shQuote(l)), intern = TRUE))
out <- grep(re0, out, value = TRUE)
if (any(grepl(instdir, out, fixed = TRUE)))
failed_fix <- TRUE
}
}
} else if (have_chrpath) {
## Linux (possibly Solaris, but there elfedit should be
## available, instead); only fixes rpaths, not DT_NEEDED
for(l in slibs) {
out <- suppressWarnings(
system(paste("chrpath", shQuote(l)), intern = TRUE))
# when multiple rpaths are present, there is a single
# RUNPATH= line with the paths separated by :
rpath <- grep(".*PATH=", out, value=TRUE)
rpath <- gsub(".*PATH=", "", rpath)
old_rpath <- rpath
# "\\$ORIGIN/.."
rpath <- gsub(instdir, quote_replacement(final_instdir),
rpath, fixed = TRUE)
if (length(rpath) && nzchar(rpath) && old_rpath != rpath) {
hardcoded_paths <- TRUE
cmd <- paste("chrpath", "-r", shQuote(rpath),
shQuote(l))
message(cmd)
ret <- suppressWarnings(system(cmd))
if (ret == 0)
message("NOTE: fixed rpath ", sQuote(old_rpath))
out <- suppressWarnings(
system(paste("chrpath", shQuote(l)), intern = TRUE))
rpath <- grep(".*PATH=", out, value = TRUE)
rpath <- gsub(".*PATH=", "", rpath)
if (any(grepl(instdir, rpath, fixed = TRUE)))
failed_fix <- TRUE
}
}
}
if (hardcoded_paths)
message("WARNING: shared objects/dynamic libraries with hard-coded temporary installation paths")
if (failed_fix)
errmsg("some hard-coded temporary paths could not be fixed")
if (have_readelf) {
## check again, needed mostly on Linux (chrpath may not be
## available or there may be DT_NEEDED entries with absolute
## paths); ldd is not suitable because it interprets $ORIGIN
for(l in slibs) {
out <- suppressWarnings(
system(paste("readelf -d", shQuote(l)), intern = TRUE))
out <- grep("^[ \t]*0x", out, value = TRUE)
if (any(grepl(instdir, out, fixed = TRUE))) {
## give path relative to installation dir
ll <- sub(file.path(instdir, ""), "", l, fixed = TRUE)
errmsg("absolute paths in ",
sQuote(ll),
" include the temporary installation directory:",
" please report to the package maintainer",
" and use ", sQuote("--no-staged-install"))
}
}
}
} # patch_rpaths()
## Make the destination directories available to the developer's
## installation scripts (e.g. configure)
Sys.setenv(R_LIBRARY_DIR = lib)
if (nzchar(lib0)) {
## FIXME: is this needed?
## set R_LIBS to include the current installation directory
rlibs <- Sys.getenv("R_LIBS")
rlibs <- if (nzchar(rlibs)) paste(lib, rlibs, sep = .Platform$path.sep) else lib
Sys.setenv(R_LIBS = rlibs)
## This is needed
.libPaths(c(lib, .libPaths()))
}
Type <- desc["Type"]
if (!is.na(Type) && Type == "Frontend") {
if (WINDOWS) errmsg("'Frontend' packages are Unix-only")
starsmsg(stars, "installing *Frontend* package ", sQuote(pkg_name), " ...")
if (preclean) system(paste(MAKE, "clean"))
if (use_configure) {
if (file_test("-x", "configure")) {
res <- system(paste(paste(configure_vars, collapse = " "),
"./configure",
paste(configure_args, collapse = " ")))
if (res) pkgerrmsg("configuration failed", pkg_name)
} else if (file.exists("configure"))
errmsg("'configure' exists but is not executable -- see the 'R Installation and Administration Manual'")
}
if (file.exists("Makefile"))
if (system(MAKE)) pkgerrmsg("make failed", pkg_name)
if (clean) system(paste(MAKE, "clean"))
return()
}
if (!is.na(Type) && Type == "Translation")
errmsg("'Translation' packages are defunct")
OS_type <- desc["OS_type"]
if (WINDOWS) {
if ((!is.na(OS_type) && OS_type == "unix") && !fake)
errmsg(" Unix-only package")
} else {
if ((!is.na(OS_type) && OS_type == "windows") && !fake)
errmsg(" Windows-only package")
}
if(group.writable) { ## group-write modes if requested:
fmode <- "664"
dmode <- "775"
} else {
fmode <- "644"
dmode <- "755"
}
## At this point we check that we have the dependencies we need.
## We cannot use installed.packages() as other installs might be
## going on in parallel
pkgInfo <- .split_description(.read_description("DESCRIPTION"))
R_install_force_depends_imports <- config_val_to_logical(Sys.getenv(
"_R_INSTALL_LIBS_ONLY_FORCE_DEPENDS_IMPORTS_", "TRUE"))
if (libs_only && isFALSE(R_install_force_depends_imports))
pkgs <- unique(c(names(names(pkgInfo$LinkingTo))))
else
pkgs <- unique(c(names(pkgInfo$Depends), names(pkgInfo$Imports),
names(pkgInfo$LinkingTo)))
if (length(pkgs)) {
miss <- character()
for (pkg in pkgs) {
if(!length(find.package(pkg, quiet = TRUE)))
miss <- c(miss, pkg)
}
if (length(miss) > 1)
pkgerrmsg(sprintf("dependencies %s are not available",
paste(sQuote(miss), collapse = ", ")),
pkg_name)
else if (length(miss))
pkgerrmsg(sprintf("dependency %s is not available",
sQuote(miss)), pkg_name)
}
starsmsg(stars, "installing *source* package ",
sQuote(pkg_name), " ...")
stars <- "**"