string oclraster_program::preprocess_code(const string& raw_code) {
	// init
	string ret_code = "";
	TCCState* state = tcc_new();
	state->output_type = TCC_OUTPUT_PREPROCESS;
	
	// split build options and let tcc parse them
	const string kernels_include_path = "-I" + core::strip_path(oclraster::kernel_path("")) + " ";
	const auto build_option_args = core::tokenize(kernels_include_path+build_options, ' ');
	const size_t argc = build_option_args.size();
	vector<const char*> argv;
	for(const auto& arg : build_option_args) {
		argv.emplace_back(arg.data());
	}
	tcc_parse_args(state, (int)argc, &argv[0]);
	
	// in-memory preprocessing
	const uint8_t* code_input = (const uint8_t*)raw_code.c_str();
	tcc_in_memory_preprocess(state, code_input, raw_code.length(), false, NULL, &ret_code,
							 [](const char* str, void* ret) -> void {
								 *(string*)ret += str;
							 });
	
	// cleanup + return
	tcc_delete(state);
	//oclr_msg("preprocessed code: %s", ret_code);
	return ret_code;
}
Example #2
0
File: tcc.c Project: raldoni/tcc
int main(int argc, char **argv)
{
    TCCState *s;
    int ret, optind, i;
    int64_t start_time = 0;

    s = tcc_new();

    optind = tcc_parse_args(s, argc - 1, argv + 1);

    if (s->do_bench)
        start_time = getclock_us();

    tcc_set_environment(s);

    if (optind == 0) {
        help();
        return 1;
    }

    if (s->option_m)
        exec_other_tcc(s, argv, s->option_m);

    if (s->verbose)
        display_info(s, 0);

    if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
        tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
        display_info(s, 1);
        return 0;
    }

    if (s->verbose && optind == 1)
        return 0;

    if (s->nb_files == 0)
        tcc_error("no input files\n");

    /* check -c consistency : only single file handled. XXX: checks file type */
    if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
        if (s->nb_libraries != 0)
            tcc_error("cannot specify libraries with -c");
        /* accepts only a single input file */
        if ((s->nb_files != 1) && s->outfile) {
            tcc_error("cannot specify multiple files with -c and -o");
        }
    }

    if (s->output_type == TCC_OUTPUT_PREPROCESS) {
        if (!s->outfile) {
            s->ppfp = stdout;
        } else {
            s->ppfp = fopen(s->outfile, "w");
            if (!s->ppfp)
                tcc_error("could not write '%s'", s->outfile);
        }
    }

    tcc_set_output_type(s, s->output_type);

    /* compile or add each files or library */
    for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
        int filetype = *(unsigned char *)s->files[i];
        const char *filename = s->files[i] + 1;
        if (filename[0] == '-' && filename[1] == 'l') {
            if (tcc_add_library(s, filename + 2) < 0) {
                tcc_error_noabort("cannot find library 'lib%s'", filename+2);
                ret = 1;
            }
        } else {
            if (1 == s->verbose)
                printf("-> %s\n", filename);
            if (!s->outfile)
                s->outfile = default_outputfile(s, filename);
            if (tcc_add_file(s, filename, filetype) < 0)
                ret = 1;
            else
            if (s->output_type == TCC_OUTPUT_OBJ) {
                ret = !!tcc_output_file(s, s->outfile);
                if (s->gen_deps && !ret)
                    gen_makedeps(s, s->outfile, s->deps_outfile);
                if (!ret) {
                    if ((i+1) < s->nb_files) {
                        tcc_delete(s);
                        s = tcc_new();
                        tcc_parse_args(s, argc - 1, argv + 1);
                        tcc_set_environment(s);
                        if (s->output_type != TCC_OUTPUT_OBJ)
                            tcc_error("interlnal error");
                        tcc_set_output_type(s, s->output_type);
                    }
                }
            }
        }
    }

    if (0 == ret) {
        if (s->output_type == TCC_OUTPUT_MEMORY) {
#ifdef TCC_IS_NATIVE
            ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
#else
            tcc_error_noabort("-run is not available in a cross compiler");
            ret = 1;
#endif
        } else
        if (s->output_type == TCC_OUTPUT_EXE ||
            s->output_type == TCC_OUTPUT_DLL)
        {
            ret = !!tcc_output_file(s, s->outfile);
            if (s->gen_deps && !ret)
                gen_makedeps(s, s->outfile, s->deps_outfile);
        }
    }

    if (s->do_bench)
        tcc_print_stats(s, getclock_us() - start_time);

    tcc_delete(s);
    return ret;
}