Escape `\` for use in `STREQUAL`?

I am trying to create a header/source from a header-only C library. To do this I need to parse C code to figure out what parts to extract out.

CMake as a language seems incredibly verbose, but I am making progress:

Which is conceptually and algorithmically similar to what I did in Python (for Python).

How do I escape the line-continuation character?

I would not recommend this. If you need to patch sources, then create a fork of the library and maintain it as a separate git repository.

@benthevining It’s my library. Planning to offer shared, static, header-only, and amalgamation-header-only variants.

(the shared/static variants are a new option, as I expect that approach to give better errors, along with the advantages of including multiple times without worrying about #define IMPL)

I mean, I could refactor the library to be header/source and then write concatenation code (which would be simpler). But I’m almost ready from the other direction route, and it would be more generalisable and thus applicable to other peoples header-only projects also.

How do I escape the line-continuation character for use in STREQUAL?

I believe you can use backslashes to escape, so an escaped backslash would be \\.

I don’t understand why you would want to offer header only and non-header-only versions of a library, it seems like a maintenance burden without much benefit. But, to each their own…

1 Like

@benthevining As you can see from the code quoted in my example, I’ve tried this:

Is there a double-quote / single-quote difference that I’m missing? Why would this not match?

I would try double quotes, most strings in CMake are double quoted

That doesn’t work, if you see in the output you still get the line-continuation character not being matched:

-- full_line = "#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) ||   \
"
-- prev_prev_char = ' '; prev_char = '\'; char = '
'
-- full_line = "    defined(__bsdi__) || defined(__DragonFly__) || defined(macintosh) ||       \
    defined(__APPLE__) || defined(__APPLE_CC__)

I don’t think single quoted strings are a thing in CMake; you should use double quotes everywhere.

As for the end goal…I think you want a slightly more powerful language that has a C parser as a library or something. CMake itself seems wholly unsuited for this problem. Maybe Python’s ffi module has something you can use? Perl probably has something on CPAN.

2 Likes