test-compile-to-dynamic-library.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "libgccjit.h"
  4. #define TEST_COMPILING_TO_FILE
  5. #define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY
  6. #define OUTPUT_FILENAME "output-of-test-compile-to-dynamic-library.c.so"
  7. #include "harness.h"
  8. void
  9. create_code (gcc_jit_context *ctxt, void *user_data)
  10. {
  11. /* Let's try to inject the equivalent of:
  12. void
  13. hello_world (const char *name)
  14. {
  15. // a test comment
  16. printf ("hello from %s\n", name);
  17. }
  18. */
  19. gcc_jit_type *void_type =
  20. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
  21. gcc_jit_type *const_char_ptr_type =
  22. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
  23. gcc_jit_param *param_name =
  24. gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
  25. gcc_jit_function *func =
  26. gcc_jit_context_new_function (ctxt, NULL,
  27. GCC_JIT_FUNCTION_EXPORTED,
  28. void_type,
  29. "hello_world",
  30. 1, &param_name,
  31. 0);
  32. gcc_jit_param *param_format =
  33. gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
  34. gcc_jit_function *printf_func =
  35. gcc_jit_context_new_function (ctxt, NULL,
  36. GCC_JIT_FUNCTION_IMPORTED,
  37. gcc_jit_context_get_type (
  38. ctxt, GCC_JIT_TYPE_INT),
  39. "printf",
  40. 1, &param_format,
  41. 1);
  42. gcc_jit_rvalue *args[2];
  43. args[0] = gcc_jit_context_new_string_literal (ctxt, "hello from %s\n");
  44. args[1] = gcc_jit_param_as_rvalue (param_name);
  45. gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
  46. gcc_jit_block_add_comment (
  47. block, NULL,
  48. "a test comment");
  49. gcc_jit_block_add_eval (
  50. block, NULL,
  51. gcc_jit_context_new_call (ctxt,
  52. NULL,
  53. printf_func,
  54. 2, args));
  55. gcc_jit_block_end_with_void_return (block, NULL);
  56. }
  57. /* { dg-final { jit-verify-output-file-was-created "" } } */
  58. /* { dg-final { jit-verify-dynamic-library "hello from ./verify-dynamic-library.c.exe" } } */