|
| 1 | +VERSION >= v"1.9" || error("This script requires Julia 1.7 or later") |
| 2 | + |
| 3 | +const headerfile_begin_regex = r"#ifdef __cplusplus\s*extern \"C\"\s*\{\s*#endif" |
| 4 | +const headerfile_end_regex = r"#ifdef __cplusplus\s*\}\s*#endif" |
| 5 | + |
| 6 | +function expand_templates(input, flintdir) |
| 7 | + matches = eachmatch(r"^@include_c_header\(\"([A-Za-z_.]+)\"\)$"m, input) |
| 8 | + substitutions = Pair{String,String}[] |
| 9 | + for m in matches |
| 10 | + filename = m.captures[1] |
| 11 | + @info "Including header \"$filename\"" |
| 12 | + template_key = "@include_c_header(\"$filename\")" |
| 13 | + |
| 14 | + # Read the file |
| 15 | + content = open(joinpath(flintdir, filename)) do headerfile |
| 16 | + read(headerfile, String) |
| 17 | + end |
| 18 | + # Restrict to the relevant part (ignore include guards etc.) |
| 19 | + begin_match = match(headerfile_begin_regex, content) |
| 20 | + @assert !isnothing(begin_match) |
| 21 | + end_match = match(headerfile_end_regex, content) |
| 22 | + @assert !isnothing(end_match) |
| 23 | + relevant_content = content[(begin_match.offset + length(begin_match.match)):(end_match.offset - 1)] |
| 24 | + # Convert to julia |
| 25 | + translated_content = c2julia(relevant_content) |
| 26 | + # Build the substitution value |
| 27 | + template_val = |
| 28 | + "###############################################################################\n" * |
| 29 | + "# begin $filename\n\n" * |
| 30 | + strip(translated_content) * |
| 31 | + "\n\n# end $filename\n" * |
| 32 | + "###############################################################################\n" |
| 33 | + push!(substitutions, template_key => template_val) |
| 34 | + end |
| 35 | + return replace(input, substitutions...) |
| 36 | +end |
| 37 | + |
| 38 | +const regex_typedef_struct_fields_name = r"typedef struct\s*\{([^{}]+)\}\s*([a-z_]+);" |
| 39 | +const regex_typedef_struct_fields_ptrname = r"typedef struct\s*\{([^{}]+)\}\s*([a-z_]+)\[1\];" |
| 40 | +const regex_struct_structname_fields = r"struct *([a-z_]+)\s*\{([^{}]+)\}\s*;" |
| 41 | +const regex_typedef_struct_structname_fields_name = r"typedef struct *([a-z_]+)\s*\{([^{}]+)\}\s*([a-z_]+);" |
| 42 | + |
| 43 | +const regex_typedef_enum_values_name = r"typedef enum\s*\{([^{}]+)\}\s*([a-z_]+);" |
| 44 | +const regex_enum_enumname_values = r"enum *([a-z_]+)\s*\{([^{}]+)\}\s*;" |
| 45 | +const regex_typedef_enum_enumname_values_name = r"typedef enum *([a-z_]+)\s*\{([^{}]+)\}\s*([a-z_]+);" |
| 46 | + |
| 47 | +function convert_struct(str) |
| 48 | + substitutions = Pair{Regex,Union{SubstitutionString, Function}}[ |
| 49 | + regex_typedef_struct_fields_name => s"struct \2\1end", # whole typedef struct construct |
| 50 | + regex_struct_structname_fields => s"struct struct_\1\2end", # whole struct construct |
| 51 | + regex_typedef_struct_fields_ptrname => s"struct struct_\2\1end\nconst \2 = Ptr{struct_\2}", # whole typedef struct construct with [1] |
| 52 | + regex_typedef_struct_structname_fields_name => s"struct \3\2end\nconst struct_\1 = \3", # whole typedef struct construct with two names |
| 53 | + r"^( +)const "m => s"\1", # remove `const` from fields |
| 54 | + r"^ +([a-z_]+) +([A-Za-z0-9_]+);"m => s" \2::\1", # simple fields (one to five declared on one line) |
| 55 | + r"^ +([a-z_]+) +([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+);"m => s" \2::\1\n \3::\1", |
| 56 | + r"^ +([a-z_]+) +([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+);"m => s" \2::\1\n \3::\1\n \4::\1", |
| 57 | + r"^ +([a-z_]+) +([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+);"m => s" \2::\1\n \3::\1\n \4::\1\n \5::\1", |
| 58 | + r"^ +([a-z_]+) +([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+);"m => s" \2::\1\n \3::\1\n \4::\1\n \5::\1\n \6::\1", |
| 59 | + r"^ +([a-z_]+) *\* *([A-Za-z0-9_]+);"m => s" \2::Ptr{\1}", # pointer field (one to five declared on one line) |
| 60 | + r"^ +([a-z_]+) *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+);"m => s" \2::Ptr{\1}\n \3::Ptr{\1}", |
| 61 | + r"^ +([a-z_]+) *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+);"m => s" \2::Ptr{\1}\n \3::Ptr{\1}\n \4::Ptr{\1}", |
| 62 | + r"^ +([a-z_]+) *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+);"m => s" \2::Ptr{\1}\n \3::Ptr{\1}\n \4::Ptr{\1}\n \5::Ptr{\1}", |
| 63 | + r"^ +([a-z_]+) *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+) *, *\* *([A-Za-z0-9_]+);"m => s" \2::Ptr{\1}\n \3::Ptr{\1}\n \4::Ptr{\1}\n \5::Ptr{\1}\n \6::Ptr{\1}", |
| 64 | + r"^ +([a-z_]+) *\* *\* *([A-Za-z0-9_]+);"m => s" \2::Ptr{Ptr{\1}}", # double pointer field |
| 65 | + r"^ +([a-z_]+) +([A-Za-z0-9_]+)\[([A-Za-z0-9_]+)\];"m => s" \2::NTuple{\3, \1}", # fixed len array field |
| 66 | + r"^ +[a-z_]+ *\( *\* *([A-Za-z0-9_]+) *\) *\([a-z_, *]+\);"m => s" \1::Ptr{Nothing}", # function pointer field |
| 67 | + r"^ +struct *([a-z_]+) +([A-Za-z0-9_]+);"m => s" \2::struct_\1", # struct field (without typedef) |
| 68 | + r"^ +enum *([a-z_]+) +([A-Za-z0-9_]+);"m => s" \2::enum_\1", # enum field (without typedef) |
| 69 | + ] |
| 70 | + for substitution in substitutions |
| 71 | + str = replace(str, substitution) |
| 72 | + end |
| 73 | + return str |
| 74 | +end |
| 75 | + |
| 76 | +function convert_enum(str) |
| 77 | + substitutions = Pair{Regex,Union{SubstitutionString, Function}}[ |
| 78 | + regex_typedef_enum_values_name => s"@enum \2 begin\1end", # whole typedef enum construct |
| 79 | + regex_enum_enumname_values => s"@enum enum_\1 begin\2end", # whole enum construct |
| 80 | + regex_typedef_enum_enumname_values_name => s"@enum \3 begin\2end\nconst enum_\1 = \3", # whole typedef enum construct with two names |
| 81 | + r"^ +([A-Za-z0-9_]+),?"m => s" \1", # simple enum values |
| 82 | + ] |
| 83 | + for substitution in substitutions |
| 84 | + str = replace(str, substitution) |
| 85 | + end |
| 86 | + return str |
| 87 | +end |
| 88 | + |
| 89 | +function c2julia(str::String) |
| 90 | + substitutions = Pair{Regex,Union{SubstitutionString, Function}}[ |
| 91 | + r"unsigned int" => s"unsigned_int", # convert `unsigned int` to a single token |
| 92 | + r"unsigned char" => s"unsigned_char", # convert `unsigned char` to a single token |
| 93 | + regex_typedef_struct_fields_name => convert_struct, # whole typedef struct construct |
| 94 | + regex_typedef_struct_fields_ptrname => convert_struct, # whole typedef struct construct with [1] |
| 95 | + regex_struct_structname_fields => convert_struct, # whole struct construct |
| 96 | + regex_typedef_struct_structname_fields_name => convert_struct, # whole typedef struct construct with two names |
| 97 | + regex_typedef_enum_values_name => convert_enum, # whole typedef enum construct |
| 98 | + regex_enum_enumname_values => convert_enum, # whole enum construct |
| 99 | + regex_typedef_enum_enumname_values_name => convert_enum, # whole typedef enum construct with two names |
| 100 | + r"^typedef ([A-Za-z_]+) ([A-Za-z_]+);"m => s"const \2 = \1", # simple typedef |
| 101 | + r"^typedef ([A-Za-z_]+) ([A-Za-z_]+)\[1\];"m => s"const \2 = Ptr{\1}", # pointer typedef |
| 102 | + r"^#define ([A-Za-z_]+) ([A-Za-z0-9_]+)"m => s"const \1 = \2", # defines used as typedef |
| 103 | + r"^//(.*)$"m => s"#\1", # line comment |
| 104 | + r"/\*(.*?)\*/"s => s"#=\1=#", # block comment |
| 105 | + ] |
| 106 | + for substitution in substitutions |
| 107 | + str = replace(str, substitution) |
| 108 | + end |
| 109 | + return str |
| 110 | +end |
| 111 | + |
| 112 | +file_header_notice(FLINT_jll_version) = """ |
| 113 | +# Most of this file is generated from FLINT's header files. |
| 114 | +# Do not edit manually, only the corresponding `*_template.jl` should be edited. |
| 115 | +
|
| 116 | +# This file was generated using FLINT_jll v$(FLINT_jll_version). |
| 117 | +
|
| 118 | +""" |
| 119 | + |
| 120 | +################################################################################ |
| 121 | +# |
| 122 | +# Main script |
| 123 | +# |
| 124 | +################################################################################ |
| 125 | + |
| 126 | +function expand_template_file(infile::String, outfile::String, flintpath::String, file_header::String) |
| 127 | + @info "Expanding file" infile outfile |
| 128 | + open(outfile, "w") do io |
| 129 | + write(io, file_header) |
| 130 | + write(io, expand_templates(read(infile, String), joinpath(flintpath, "include", "flint"))) |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +function main() |
| 135 | + flintpath = FLINT_jll.find_artifact_dir() |
| 136 | + flintversion = pkgversion(FLINT_jll) |
| 137 | + @info "Found FLINT" flintpath flintversion |
| 138 | + |
| 139 | + infile = joinpath(nemopath, "src", "flint", "FlintCTypes_template.jl") |
| 140 | + outfile = joinpath(nemopath, "src", "flint", "FlintCTypes.jl") |
| 141 | + expand_template_file(infile, outfile, flintpath, file_header_notice(flintversion)) |
| 142 | +end |
| 143 | + |
| 144 | +const nemopath = dirname(dirname(@__FILE__)) |
| 145 | + |
| 146 | +@info "Setting up environment" |
| 147 | +using Pkg |
| 148 | +Pkg.activate(; temp=true) |
| 149 | +Pkg.develop(PackageSpec(; path=nemopath)) |
| 150 | +Pkg.add("FLINT_jll") # version is fixed by Nemo.jl in the line above |
| 151 | +using FLINT_jll |
| 152 | + |
| 153 | +main() |
0 commit comments