]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/resource.c
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/gcc.git] / gcc / java / resource.c
1 /* Functions related to building resource files.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009, 2010, 2012 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tree.h"
29 #include "java-tree.h"
30 #include "jcf.h"
31 #include "diagnostic-core.h"
32 #include "toplev.h"
33 #include "parse.h"
34 #include "function.h"
35 #include "ggc.h"
36 #include "tree-iterator.h"
37 #include "cgraph.h"
38
39 /* A list of all the resources files. */
40 static GTY(()) vec<tree, va_gc> *resources;
41
42 void
43 compile_resource_data (const char *name, const char *buffer, int length)
44 {
45 tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
46 vec<constructor_elt, va_gc> *v = NULL;
47
48 data_type = build_prim_array_type (unsigned_byte_type_node,
49 strlen (name) + length);
50 rtype = make_node (RECORD_TYPE);
51 PUSH_FIELD (input_location,
52 rtype, field, "name_length", unsigned_int_type_node);
53 PUSH_FIELD (input_location,
54 rtype, field, "resource_length", unsigned_int_type_node);
55 PUSH_FIELD (input_location, rtype, field, "data", data_type);
56 FINISH_RECORD (rtype);
57 START_RECORD_CONSTRUCTOR (v, rtype);
58 PUSH_FIELD_VALUE (v, "name_length",
59 build_int_cst (NULL_TREE, strlen (name)));
60 PUSH_FIELD_VALUE (v, "resource_length",
61 build_int_cst (NULL_TREE, length));
62 data = build_string (strlen(name) + length, buffer);
63 TREE_TYPE (data) = data_type;
64 PUSH_FIELD_VALUE (v, "data", data);
65 FINISH_RECORD_CONSTRUCTOR (rinit, v, rtype);
66 TREE_CONSTANT (rinit) = 1;
67
68 decl = build_decl (input_location,
69 VAR_DECL, java_mangle_resource_name (name), rtype);
70 TREE_STATIC (decl) = 1;
71 TREE_PUBLIC (decl) = 1;
72 java_hide_decl (decl);
73 DECL_ARTIFICIAL (decl) = 1;
74 DECL_IGNORED_P (decl) = 1;
75 TREE_READONLY (decl) = 1;
76 TREE_THIS_VOLATILE (decl) = 0;
77 DECL_INITIAL (decl) = rinit;
78 layout_decl (decl, 0);
79 pushdecl (decl);
80 rest_of_decl_compilation (decl, global_bindings_p (), 0);
81 varpool_finalize_decl (decl);
82
83 vec_safe_push (resources, decl);
84 }
85
86 void
87 write_resource_constructor (tree *list_p)
88 {
89 tree decl, t, register_resource_fn;
90 unsigned ix;
91
92 if (resources == NULL)
93 return;
94
95 t = build_function_type_list (void_type_node, ptr_type_node, NULL);
96 t = build_decl (input_location,
97 FUNCTION_DECL, get_identifier ("_Jv_RegisterResource"), t);
98 TREE_PUBLIC (t) = 1;
99 DECL_EXTERNAL (t) = 1;
100 register_resource_fn = t;
101
102 /* Write out entries in the same order in which they were defined. */
103 FOR_EACH_VEC_ELT (*resources, ix, decl)
104 {
105 t = build_fold_addr_expr (decl);
106 t = build_call_expr (register_resource_fn, 1, t);
107 append_to_statement_list (t, list_p);
108 }
109 }
110
111 /* Generate a byte array representing the contents of FILENAME. The
112 array is assigned a unique local symbol. The array represents a
113 compiled Java resource, which is accessed by the runtime using
114 NAME. */
115 void
116 compile_resource_file (const char *name, const char *filename)
117 {
118 struct stat stat_buf;
119 int fd;
120 char *buffer;
121
122 fd = open (filename, O_RDONLY | O_BINARY);
123 if (fd < 0)
124 {
125 perror ("Failed to read resource file");
126 return;
127 }
128 if (fstat (fd, &stat_buf) != 0
129 || ! S_ISREG (stat_buf.st_mode))
130 {
131 perror ("Could not figure length of resource file");
132 return;
133 }
134 buffer = XNEWVEC (char, strlen (name) + stat_buf.st_size);
135 strcpy (buffer, name);
136 read (fd, buffer + strlen (name), stat_buf.st_size);
137 close (fd);
138
139 compile_resource_data (name, buffer, stat_buf.st_size);
140 }
141
142 #include "gt-java-resource.h"