summaryrefslogtreecommitdiff
path: root/meta/recipes-graphics/fontconfig/files/Add-sysroot-option-to-fc-cache-and-fc-cat.patch
blob: f0b3b7fc3287481524ed1b7219da09b394c96250 (plain)
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
Upstream-Status: Pending

From a5eeeafb623a5508d2745f89aaf69118799f7e5c Mon Sep 17 00:00:00 2001
From: Laurentiu Palcu <laurentiu.palcu@intel.com>
Date: Mon, 28 Jan 2013 11:42:56 +0200
Subject: [PATCH] Add sysroot option to fc-cache and fc-cat

Whether one needs to generate the font cache offline and then deploy the
image to a target or do some testing in a separate rootfs, the sysroot
option will facilitate that.

Suppose you've got a rootfs in the following directory:
/path/to/test/rootfs. In order to contain the fc-cache generation to
that particular directory, the following command can be used:

fc-cache --sysroot=/path/to/test/rootfs

That will make fc-cache to prepend the sysroot directory to all paths
during scanning. For example, instead of searching /etc/fonts/ directory
for configuration files, it will look in /path/to/test/rootfs/etc/fonts.
The paths found in fonts.conf will also be prepended with the sysroot.

However, the generated cache files will not contain any references to
sysroot. This way, one can generate the font cache offline and then deploy
the image to target. Or, simply, use it for various tests without
polluting the system/user cache files.

In order to inspect the cache generated using the sysroot option, one
has to use fc-cat like below (for example):

fc-cat --sysroot=/path/to/test/rootfs
/path/to/test/rootfs/var/cache/fontconfig/*

Signed-off-by: Laurentiu Palcu <laurentiu.palcu@intel.com>
---
 fc-cache/fc-cache.c     |   63 ++++++++++++++++++++++++++++-------
 fc-cat/fc-cat.c         |   77 +++++++++++++++++++++++++++++++++++-------
 fc-lang/fc-lang.c       |    1 +
 fontconfig/fontconfig.h |    6 ++++
 src/fccache.c           |   85 +++++++++++++++++++++++++++++++++++++++++++----
 src/fccfg.c             |   52 +++++++++++++++++++++++++++++
 src/fcfreetype.c        |    4 +++
 src/fcstr.c             |   27 +++++++++++++++
 8 files changed, 285 insertions(+), 30 deletions(-)

diff --git a/fc-cache/fc-cache.c b/fc-cache/fc-cache.c
index 9fb383b..a91e1f1 100644
--- a/fc-cache/fc-cache.c
+++ b/fc-cache/fc-cache.c
@@ -68,6 +68,7 @@ const struct option longopts[] = {
     {"force", 0, 0, 'f'},
     {"really-force", 0, 0, 'r'},
     {"system-only", 0, 0, 's'},
+    {"sysroot", 1, 0, 'y'},
     {"version", 0, 0, 'V'},
     {"verbose", 0, 0, 'v'},
     {"help", 0, 0, 'h'},
@@ -85,26 +86,28 @@ usage (char *program, int error)
 {
     FILE *file = error ? stderr : stdout;
 #if HAVE_GETOPT_LONG
-    fprintf (file, "usage: %s [-frsvVh] [--force|--really-force] [--system-only] [--verbose] [--version] [--help] [dirs]\n",
+    fprintf (file, "usage: %s [-frsvVh] [-y SYSROOT] [--force|--really-force] [--system-only] [--sysroot=SYSROOT] [--verbose] [--version] [--help] [dirs]\n",
 	     program);
 #else
-    fprintf (file, "usage: %s [-frsvVh] [dirs]\n",
+    fprintf (file, "usage: %s [-frsvVh] [-y SYSROOT] [dirs]\n",
 	     program);
 #endif
     fprintf (file, "Build font information caches in [dirs]\n"
 	     "(all directories in font configuration by default).\n");
     fprintf (file, "\n");
 #if HAVE_GETOPT_LONG
-    fprintf (file, "  -f, --force          scan directories with apparently valid caches\n");
-    fprintf (file, "  -r, --really-force   erase all existing caches, then rescan\n");
-    fprintf (file, "  -s, --system-only    scan system-wide directories only\n");
-    fprintf (file, "  -v, --verbose        display status information while busy\n");
-    fprintf (file, "  -V, --version        display font config version and exit\n");
-    fprintf (file, "  -h, --help           display this help and exit\n");
+    fprintf (file, "  -f, --force           scan directories with apparently valid caches\n");
+    fprintf (file, "  -r, --really-force    erase all existing caches, then rescan\n");
+    fprintf (file, "  -s, --system-only     scan system-wide directories only\n");
+    fprintf (file, "  -y, --sysroot=SYSROOT for scanning, prefix all paths with SYSROOT. The cache file will not contain the SYSROOT!\n");
+    fprintf (file, "  -v, --verbose         display status information while busy\n");
+    fprintf (file, "  -V, --version         display font config version and exit\n");
+    fprintf (file, "  -h, --help            display this help and exit\n");
 #else
     fprintf (file, "  -f         (force)   scan directories with apparently valid caches\n");
     fprintf (file, "  -r,   (really force) erase all existing caches, then rescan\n");
     fprintf (file, "  -s         (system)  scan system-wide directories only\n");
+    fprintf (file, "  -y SYSROOT           for scanning, prefix all paths with SYSROOT. The cache file will not contain the SYSROOT!\n");
     fprintf (file, "  -v         (verbose) display status information while busy\n");
     fprintf (file, "  -V         (version) display font config version and exit\n");
     fprintf (file, "  -h         (help)    display this help and exit\n");
@@ -125,7 +128,7 @@ scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force,
     struct stat	    statb;
     FcBool	    was_valid;
     int		    i;
-    
+    FcChar8	    *sysroot = FcConfigGetSysRoot ();
     /*
      * Now scan all of the directories into separate databases
      * and write out the results
@@ -227,7 +230,22 @@ scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force,
 	    continue;
 	}
 	for (i = 0; i < FcCacheNumSubdir (cache); i++)
-	    FcStrSetAdd (subdirs, FcCacheSubdir (cache, i));
+	{
+	    const FcChar8 *subdir = FcCacheSubdir (cache, i);
+	    if (sysroot)
+	    {
+		subdir = FcStrPlus (sysroot, subdir);
+		if (!subdir)
+		{
+		    fprintf (stderr, "malloc failure\n");
+		    return ++ret;
+		}
+	    }
+
+	    FcStrSetAdd (subdirs, subdir);
+	    if (sysroot)
+		FcStrFree ((FcChar8 *) subdir);
+	}
 	
 	FcDirCacheUnload (cache);
 	
@@ -277,6 +295,7 @@ main (int argc, char **argv)
     FcBool	really_force = FcFalse;
     FcBool	systemOnly = FcFalse;
     FcConfig	*config;
+    FcChar8 *sysroot = NULL;
     int		i;
     int		changed;
     int		ret;
@@ -284,9 +303,9 @@ main (int argc, char **argv)
     int		c;
 
 #if HAVE_GETOPT_LONG
-    while ((c = getopt_long (argc, argv, "frsVvh", longopts, NULL)) != -1)
+    while ((c = getopt_long (argc, argv, "frsy:Vvh", longopts, NULL)) != -1)
 #else
-    while ((c = getopt (argc, argv, "frsVvh")) != -1)
+    while ((c = getopt (argc, argv, "frsy:Vvh")) != -1)
 #endif
     {
 	switch (c) {
@@ -299,6 +318,9 @@ main (int argc, char **argv)
 	case 's':
 	    systemOnly = FcTrue;
 	    break;
+	case 'y':
+	    sysroot = FcStrCopy ((const FcChar8*) optarg);
+	    break;
 	case 'V':
 	    fprintf (stderr, "fontconfig version %d.%d.%d\n", 
 		     FC_MAJOR, FC_MINOR, FC_REVISION);
@@ -319,6 +341,21 @@ main (int argc, char **argv)
 
     if (systemOnly)
 	FcConfigEnableHome (FcFalse);
+
+    if (sysroot)
+    {
+	FcChar8 *canon_sysroot;
+	canon_sysroot = FcConfigSetSysRoot(sysroot);
+	FcStrFree (sysroot);
+	if (!canon_sysroot)
+	{
+	    fprintf (stderr, "Cannot set the sysroot. Out of memory!\n");
+	    return 1;
+	}
+
+	sysroot = canon_sysroot;
+    }
+
     config = FcInitLoadConfig ();
     if (!config)
     {
@@ -378,6 +415,8 @@ main (int argc, char **argv)
      * library, and there aren't any signals flying around here.
      */
     FcConfigDestroy (config);
+    if (sysroot)
+	FcStrFree (sysroot);
     FcFini ();
     if (changed)
 	sleep (2);
diff --git a/fc-cat/fc-cat.c b/fc-cat/fc-cat.c
index 72912b7..4b4f0f0 100644
--- a/fc-cat/fc-cat.c
+++ b/fc-cat/fc-cat.c
@@ -57,6 +57,7 @@ const struct option longopts[] = {
     {"verbose", 0, 0, 'v'},
     {"recurse", 0, 0, 'r'},
     {"help", 0, 0, 'h'},
+    {"sysroot", 1, 0, 'y'},
     {NULL,0,0,0},
 };
 #else
@@ -150,11 +151,11 @@ usage (char *program, int error)
 {
     FILE *file = error ? stderr : stdout;
 #if HAVE_GETOPT_LONG
-    fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [*-%s" FC_CACHE_SUFFIX "|directory]...\n",
+    fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [--sysroot=SYSROOT] [*-%s" FC_CACHE_SUFFIX "|directory]...\n",
 	     program, FC_ARCHITECTURE);
     fprintf (file, "       %s [-Vh] [--version] [--help]\n", program);
 #else
-    fprintf (file, "usage: %s [-rvVh] [*-%s" FC_CACHE_SUFFIX "|directory]...\n",
+    fprintf (file, "usage: %s [-rvVh] [-y SYSROOT] [*-%s" FC_CACHE_SUFFIX "|directory]...\n",
 	     program, FC_ARCHITECTURE);
 #endif
     fprintf (file, "Reads font information cache from:\n");
@@ -162,15 +163,17 @@ usage (char *program, int error)
     fprintf (file, " 2) related to a particular font directory\n");
     fprintf (file, "\n");
 #if HAVE_GETOPT_LONG
-    fprintf (file, "  -r, --recurse        recurse into subdirectories\n");
-    fprintf (file, "  -v, --verbose        be verbose\n");
-    fprintf (file, "  -V, --version        display font config version and exit\n");
-    fprintf (file, "  -h, --help           display this help and exit\n");
+    fprintf (file, "  -r, --recurse         recurse into subdirectories\n");
+    fprintf (file, "  -v, --verbose         be verbose\n");
+    fprintf (file, "  -V, --version         display font config version and exit\n");
+    fprintf (file, "  -h, --help            display this help and exit\n");
+    fprintf (file, "  -y, --sysroot=SYSROOT needed if the cache was generated using --sysroot\n");
 #else
     fprintf (file, "  -r         (recurse) recurse into subdirectories\n");
     fprintf (file, "  -v         (verbose) be verbose\n");
     fprintf (file, "  -V         (version) display font config version and exit\n");
     fprintf (file, "  -h         (help)    display this help and exit\n");
+    fprintf (file, "  -y SYSROOT           needed if the cache was generated using --sysroot\n");
 #endif
     exit (error);
 }
@@ -262,13 +265,14 @@ main (int argc, char **argv)
     int		verbose = 0;
     int		recurse = 0;
     FcBool	first = FcTrue;
+    FcChar8	*sysroot = NULL;
 #if HAVE_GETOPT_LONG || HAVE_GETOPT
     int		c;
 
 #if HAVE_GETOPT_LONG
-    while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1)
+    while ((c = getopt_long (argc, argv, "Vvrhy:", longopts, NULL)) != -1)
 #else
-    while ((c = getopt (argc, argv, "Vvrh")) != -1)
+    while ((c = getopt (argc, argv, "Vvrhy:")) != -1)
 #endif
     {
 	switch (c) {
@@ -284,6 +288,9 @@ main (int argc, char **argv)
 	    break;
 	case 'h':
 	    usage (argv[0], 0);
+	case 'y':
+	    sysroot = FcStrCopy ((const FcChar8*) optarg);
+	    break;
 	default:
 	    usage (argv[0], 1);
 	}
@@ -293,6 +300,20 @@ main (int argc, char **argv)
     i = 1;
 #endif
 
+    if (sysroot)
+    {
+	FcChar8 *canon_sysroot;
+	canon_sysroot = FcConfigSetSysRoot(sysroot);
+	FcStrFree (sysroot);
+	if (!canon_sysroot)
+	{
+	    fprintf (stderr, "%s: malloc failure\n", argv[0]);
+	    return 1;
+	}
+
+	sysroot = canon_sysroot;
+    }
+
     config = FcInitLoadConfig ();
     if (!config)
     {
@@ -348,6 +369,7 @@ main (int argc, char **argv)
 	int	    j;
 	FcChar8	    *cache_file = NULL;
 	struct stat file_stat;
+	const FcChar8 *base_dir = NULL;
 	
 	if (FcFileIsDir (arg))
 	    cache = FcDirCacheLoad (arg, config, &cache_file);
@@ -364,9 +386,34 @@ main (int argc, char **argv)
 	fs = FcCacheCopySet (cache);
 	for (j = 0; j < FcCacheNumSubdir (cache); j++) 
 	{
-	    FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
+	    const FcChar8 *subdir = FcCacheSubdir (cache, j);
+	    if (sysroot)
+	    {
+		subdir = FcStrPlus (sysroot, subdir);
+		if (!subdir)
+		{
+		    fprintf (stderr, "%s: malloc failure\n", argv[0]);
+		    return 1;
+		}
+	    }
+
+	    FcStrSetAdd (dirs, subdir);
 	    if (recurse)
-		FcStrSetAdd (args, FcCacheSubdir (cache, j));
+		FcStrSetAdd (args, subdir);
+
+	    if (sysroot)
+		FcStrFree ((FcChar8 *) subdir);
+	}
+
+	base_dir = FcCacheDir (cache);
+	if (sysroot)
+	{
+	    base_dir = FcStrPlus (sysroot, base_dir);
+	    if (!base_dir)
+	    {
+		fprintf (stderr, "%s: malloc failure\n", argv[0]);
+		return 1;
+	    }
 	}
 
 	if (verbose)
@@ -374,10 +421,13 @@ main (int argc, char **argv)
 	    if (!first)
 		printf ("\n");
 	    printf ("Directory: %s\nCache: %s\n--------\n",
-		    FcCacheDir(cache), cache_file ? cache_file : arg);
+		    base_dir, cache_file ? cache_file : arg);
 	    first = FcFalse;
 	}
-        cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
+        cache_print_set (fs, dirs, base_dir, verbose);
+
+	if (sysroot)
+	    FcStrFree ((FcChar8 *) base_dir);
 
 	FcStrSetDestroy (dirs);
 
@@ -387,6 +437,9 @@ main (int argc, char **argv)
 	    FcStrFree (cache_file);
     }
 
+    if (sysroot)
+	FcStrFree (sysroot);
+
     FcFini ();
     return 0;
 }
diff --git a/fc-lang/fc-lang.c b/fc-lang/fc-lang.c
index 93200c4..e74e856 100644
--- a/fc-lang/fc-lang.c
+++ b/fc-lang/fc-lang.c
@@ -22,6 +22,7 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
+#define FC_LANG_C
 #include "fccharset.c"
 #include "fcstr.c"
 #include "fcserialize.c"
diff --git a/fontconfig/fontconfig.h b/fontconfig/fontconfig.h
index 266986c..b05f6ca 100644
--- a/fontconfig/fontconfig.h
+++ b/fontconfig/fontconfig.h
@@ -417,6 +417,12 @@ FcConfigSubstitute (FcConfig	*config,
 		    FcPattern	*p,
 		    FcMatchKind	kind);
 
+FcPublic FcChar8 *
+FcConfigSetSysRoot (const FcChar8 *sysroot);
+
+FcPublic FcChar8 *
+FcConfigGetSysRoot (void);
+
 /* fccharset.c */
 FcPublic FcCharSet*
 FcCharSetCreate (void);
diff --git a/src/fccache.c b/src/fccache.c
index 81985df..c3dcc72 100644
--- a/src/fccache.c
+++ b/src/fccache.c
@@ -93,6 +93,14 @@ FcDirCacheBasename (const FcChar8 * dir, FcChar8 cache_base[CACHEBASE_LEN])
     FcChar8		*hex_hash;
     int			cnt;
     struct MD5Context 	ctx;
+    FcChar8		*sysroot = FcConfigGetSysRoot();
+
+    /*
+     * remove sysroot when generating the hex hash
+     */
+    if (sysroot && !strncmp ((const char*) sysroot, (const char*) dir,
+		strlen ((const char*) sysroot)))
+	    dir += strlen((const char*) sysroot);
 
     MD5Init (&ctx);
     MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
@@ -505,16 +513,31 @@ static FcBool
 FcCacheTimeValid (FcCache *cache, struct stat *dir_stat)
 {
     struct stat	dir_static;
+    FcChar8 *dir = FcCacheDir (cache);
+    FcChar8 *sysroot = FcConfigGetSysRoot ();
+
+    if (sysroot)
+    {
+	dir = FcStrPlus (sysroot, dir);
+	if (!dir)
+	    return FcFalse;
+    }
 
     if (!dir_stat)
     {
-	if (FcStatChecksum (FcCacheDir (cache), &dir_static) < 0)
+	if (FcStatChecksum (dir, &dir_static) < 0)
+	{
+	    if (sysroot)
+		FcStrFree (dir);
 	    return FcFalse;
+	}
 	dir_stat = &dir_static;
     }
     if (FcDebug () & FC_DBG_CACHE)
 	printf ("FcCacheTimeValid dir \"%s\" cache checksum %d dir checksum %d\n",
-		FcCacheDir (cache), cache->checksum, (int) dir_stat->st_mtime);
+		dir, cache->checksum, (int) dir_stat->st_mtime);
+    if (sysroot)
+	FcStrFree (dir);
     return cache->checksum == (int) dir_stat->st_mtime;
 }
 
@@ -716,9 +739,27 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt
     FcChar8	*dir_serialize;
     intptr_t	*dirs_serialize;
     FcFontSet	*set_serialize;
+    FcChar8	*sysroot = FcConfigGetSysRoot ();
+    FcStrSet	*dirs_without_sysroot;
 
     if (!serialize)
 	return NULL;
+
+    if (sysroot)
+    {
+	dir += strlen ((const char*) sysroot);
+
+	dirs_without_sysroot = FcStrSetCreate ();
+	if (!dirs_without_sysroot)
+	    return NULL;
+
+	for (i = 0; i < dirs->num; i++)
+	    FcStrSetAdd (dirs_without_sysroot,
+		dirs->strs[i] + strlen ((const char*) sysroot));
+
+	dirs = dirs_without_sysroot;
+    }
+
     /*
      * Space for cache structure
      */
@@ -792,11 +833,17 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, struct stat *dir_stat, FcSt
 
     FcCacheInsert (cache, NULL);
 
+    if (sysroot)
+	FcStrSetDestroy(dirs_without_sysroot);
+
     return cache;
 
 bail2:
     free (cache);
 bail1:
+    if (sysroot)
+	FcStrSetDestroy(dirs_without_sysroot);
+
     FcSerializeDestroy (serialize);
     return NULL;
 }
@@ -844,6 +891,14 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     struct stat     cache_stat;
     int		    magic;
     int		    written;
+    FcChar8	    *sysroot = FcConfigGetSysRoot ();
+
+    if (sysroot)
+    {
+	dir = FcStrPlus (sysroot, dir);
+	if (!dir)
+	    return FcFalse;
+    }
 
     /*
      * Write it to the first directory in the list which is writable
@@ -851,7 +906,7 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
 
     list = FcStrListCreate (config->cacheDirs);
     if (!list)
-	return FcFalse;
+	goto bail0;
     while ((test_dir = FcStrListNext (list))) {
 	if (access ((char *) test_dir, W_OK) == 0)
 	{
@@ -886,12 +941,12 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     }
     FcStrListDone (list);
     if (!cache_dir)
-	return FcFalse;
+	goto bail0;
 
     FcDirCacheBasename (dir, cache_base);
     cache_hashed = FcStrPlus (cache_dir, cache_base);
     if (!cache_hashed)
-        return FcFalse;
+        goto bail0;
 
     if (FcDebug () & FC_DBG_CACHE)
         printf ("FcDirCacheWriteDir dir \"%s\" file \"%s\"\n",
@@ -948,6 +1003,8 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     FcStrFree (cache_hashed);
     FcAtomicUnlock (atomic);
     FcAtomicDestroy (atomic);
+    if (sysroot)
+	FcStrFree (dir);
     return FcTrue;
 
  bail5:
@@ -958,6 +1015,9 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
     FcAtomicDestroy (atomic);
  bail1:
     FcStrFree (cache_hashed);
+ bail0:
+    if (sysroot)
+	FcStrFree (dir);
     return FcFalse;
 }
 
@@ -997,7 +1057,8 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
     while ((ent = readdir (d)))
     {
 	FcChar8	*file_name;
-	const FcChar8	*target_dir;
+	FcChar8	*target_dir;
+	FcChar8	*sysroot = FcConfigGetSysRoot ();
 
 	if (ent->d_name[0] == '.')
 	    continue;
@@ -1025,6 +1086,16 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
 	else
 	{
 	    target_dir = FcCacheDir (cache);
+	    if (sysroot)
+	    {
+		target_dir = FcStrPlus (sysroot, target_dir);
+		if (!target_dir)
+		{
+		    ret = FcFalse;
+		    FcStrFree (file_name);
+		    break;
+		}
+	    }
 	    if (stat ((char *) target_dir, &target_stat) < 0)
 	    {
 		if (verbose || FcDebug () & FC_DBG_CACHE)
@@ -1043,6 +1114,8 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
 	}
 	FcDirCacheUnload (cache);
         FcStrFree (file_name);
+	if (sysroot)
+	    FcStrFree (target_dir);
     }
 
     closedir (d);
diff --git a/src/fccfg.c b/src/fccfg.c
index d3752e5..ad97c05 100644
--- a/src/fccfg.c
+++ b/src/fccfg.c
@@ -37,6 +37,7 @@
 #endif
 
 FcConfig    *_fcConfig;
+static FcChar8 *_FcConfigSysRoot = NULL;
 
 FcConfig *
 FcConfigCreate (void)
@@ -1716,6 +1717,7 @@ FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
 {
     FcChar8    *path;
     int         size, osize;
+    FcChar8    *sysroot = _FcConfigSysRoot;
 
     if (!dir)
 	dir = (FcChar8 *) "";
@@ -1747,6 +1749,19 @@ FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
     strcat ((char *) path, (char *) file);
 
     FcMemAlloc (FC_MEM_STRING, osize);
+
+    if (sysroot &&
+            strncmp ((const char*) sysroot, (const char*) path,
+                    strlen ((const char *) sysroot)))
+    {
+        FcChar8 *new_path = FcStrPlus (sysroot, path);
+        FcStrFree (path);
+        if (!new_path)
+            return 0;
+
+        path = new_path;
+    }
+
     if (access ((char *) path, R_OK) == 0)
 	return path;
 
@@ -2217,6 +2232,43 @@ FcConfigAcceptFont (FcConfig	    *config,
 	return FcFalse;
     return FcTrue;
 }
+
+
+FcPublic FcChar8 *
+FcConfigSetSysRoot (const FcChar8 *sysroot)
+{
+    FcChar8 *old_sysroot, *new_sysroot;
+
+    if (!sysroot)
+	return NULL;
+
+    new_sysroot = FcStrCopyFilename(sysroot);
+    if (!new_sysroot)
+	return NULL;
+
+    old_sysroot = _FcConfigSysRoot;
+
+    if (old_sysroot &&
+	    !strcmp ((const char *) new_sysroot, (const char *) old_sysroot))
+    {
+	FcStrFree (new_sysroot);
+	return old_sysroot;
+    }
+
+    _FcConfigSysRoot = new_sysroot;
+
+    if (old_sysroot)
+	FcStrFree (old_sysroot);
+
+    return new_sysroot;
+}
+
+FcPublic FcChar8 *
+FcConfigGetSysRoot (void)
+{
+    return _FcConfigSysRoot;
+}
+
 #define __fccfg__
 #include "fcaliastail.h"
 #undef __fccfg__
diff --git a/src/fcfreetype.c b/src/fcfreetype.c
index fb2b0f2..c497be5 100644
--- a/src/fcfreetype.c
+++ b/src/fcfreetype.c
@@ -1124,6 +1124,7 @@ FcFreeTypeQueryFace (const FT_Face  face,
 
     FcChar8	    *style = 0;
     int		    st;
+    FcChar8	    *sysroot = FcConfigGetSysRoot();
 
     pat = FcPatternCreate ();
     if (!pat)
@@ -1340,6 +1341,9 @@ FcFreeTypeQueryFace (const FT_Face  face,
 	++nstyle;
     }
 
+    if (sysroot)
+	file += strlen ((const char*) sysroot);
+
     if (!nfamily)
     {
 	FcChar8	*start, *end;
diff --git a/src/fcstr.c b/src/fcstr.c
index 037960d..62ceae6 100644
--- a/src/fcstr.c
+++ b/src/fcstr.c
@@ -1170,6 +1170,8 @@ FcBool
 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
 {
     FcChar8 *new = FcStrCopyFilename (s);
+
+#ifdef FC_LANG_C
     if (!new)
 	return FcFalse;
     if (!_FcStrSetAppend (set, new))
@@ -1177,6 +1179,31 @@ FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
 	FcStrFree (new);
 	return FcFalse;
     }
+#else
+    FcChar8 *full;
+    FcChar8 *sysroot = FcConfigGetSysRoot();
+
+    if (!new)
+	return FcFalse;
+
+    if (sysroot && strncmp ((const char *) sysroot, (const char *) new,
+		strlen ((const char*) sysroot)))
+    {
+	    full = FcStrPlus(sysroot, new);
+	    FcStrFree(new);
+	    if (!full)
+		return FcFalse;
+    }
+    else
+	full = new;
+
+    if (!_FcStrSetAppend (set, full))
+    {
+	FcStrFree (full);
+	return FcFalse;
+    }
+#endif
+
     return FcTrue;
 }
 
-- 
1.7.9.5