resource.c 4.7 KB

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