Skip to content

Commit

Permalink
Do not #include platform.h in headers
Browse files Browse the repository at this point in the history
platform.h includes config.h which is a default name for any
project's configuration definitions. We could use a more specific
name, but every config.h defines things like PACKAGE_NAME and
thus including two such files results in compilation warnings or
errors.

The general rule is that only .c files should include
config.h. When built and installed, the library should provide
library files (static or dynamic) and headers defining the API,
but not config.h.
  • Loading branch information
vpodzime committed Sep 15, 2023
1 parent e7660b7 commit a97533a
Show file tree
Hide file tree
Showing 41 changed files with 220 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ stamp-h1
/libutils/config.h
/libutils/config.h.in
/libutils/config.post.h
/libutils/writer.h
/libutils/clockid_t.h
/libutils/regex.h
/libutils/json-yaml.h

# examples
/examples/cfengine_stdlib.cf
Expand Down
28 changes: 24 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,12 @@ if test "x$with_pcre" != "xno"; then
])
AC_DEFINE(WITH_PCRE, 1, [Define if PCRE is being used])
AM_CONDITIONAL(WITH_PCRE, true)
WITH_PCRE_DEFINE="#define WITH_PCRE 1"
else
AM_CONDITIONAL(WITH_PCRE, false)
WITH_PCRE_DEFINE="// #undef WITH_PCRE"
fi
AC_SUBST(WITH_PCRE_DEFINE)

dnl systemd structured logging

Expand All @@ -336,13 +339,17 @@ if test "x$with_libyaml" != xno
then
CF3_WITH_LIBRARY(libyaml, [
AC_CHECK_LIB(yaml, yaml_parser_initialize,
[],
[if test "x$with_libyaml" != xcheck; then AC_MSG_ERROR(Cannot find libyaml library); fi])
[HAVE_LIBYAML_DEFINE="#define HAVE_LIBYAML 1"],
[if test "x$with_libyaml" != xcheck; then AC_MSG_ERROR(Cannot find libyaml library); fi
HAVE_LIBYAML_DEFINE="// #undef HAVE_LIBYAML"])
AC_CHECK_HEADERS(yaml.h,
[libyaml_header_found=yes],
[if test "x$with_libyaml" != xcheck; then AC_MSG_ERROR(Cannot find libyaml header files); fi])
])
else
HAVE_LIBYAML_DEFINE="// #undef HAVE_LIBYAML"
fi
AC_SUBST(HAVE_LIBYAML_DEFINE)

dnl ######################################################################
dnl Checks for header files.
Expand All @@ -366,7 +373,12 @@ AC_CHECK_HEADERS(net/if_arp.h, , , [AC_INCLUDES_DEFAULT
#include <sys/socket.h>
])

AC_CHECK_HEADERS(getopt.h)
AC_CHECK_HEADERS(getopt.h,
[HAVE_GETOPT_H_DEFINE="#define HAVE_GETOPT_H 1"],
[HAVE_GETOPT_H_DEFINE="// #undef HAVE_GETOPT_H"]
)
AC_SUBST(HAVE_GETOPT_H_DEFINE)

AC_CHECK_HEADERS(utime.h)
AC_CHECK_HEADERS(time.h)
AC_CHECK_HEADERS(sys/time.h)
Expand Down Expand Up @@ -411,11 +423,15 @@ AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_TYPE_PID_T
AC_CHECK_TYPES(clockid_t, [], [], [[
AC_CHECK_TYPES(clockid_t,
[HAVE_CLOCKID_T_DEFINE="#define HAVE_CLOCKID_T 1"],
[HAVE_CLOCKID_T_DEFINE="// #undef HAVE_CLOCKID_T"]
, [[
#ifdef HAVE_TIME_H
# include <time.h>
#endif
]])
AC_SUBST(HAVE_CLOCKID_T_DEFINE)

AC_CHECK_TYPES(socklen_t, [], [], [[
#ifdef HAVE_SYS_TYPES_H
Expand Down Expand Up @@ -1244,6 +1260,10 @@ dnl ######################################################################
AC_CONFIG_FILES([Makefile
libcompat/Makefile
libutils/Makefile
libutils/writer.h
libutils/clockid_t.h
libutils/regex.h
libutils/json-yaml.h
config.post.h
tests/Makefile
tests/unit/Makefile])
Expand Down
2 changes: 2 additions & 0 deletions libutils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ libutils_la_SOURCES = \
array_map.c array_map_priv.h \
buffer.c buffer.h \
cleanup.c cleanup.h \
clockid_t.h \
compiler.h \
csv_writer.c csv_writer.h \
csv_parser.c csv_parser.h \
Expand All @@ -58,6 +59,7 @@ libutils_la_SOURCES = \
mutex.c mutex.h \
passopenfile.c passopenfile.h \
path.c path.h \
path_max.h \
platform.h condition_macros.h \
printsize.h \
proc_keyvalue.c proc_keyvalue.h \
Expand Down
36 changes: 36 additions & 0 deletions libutils/clockid_t.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023 Northern.tech AS

This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/

#ifndef CLOCKID_T_H
#define CLOCKID_T_H

/* Set by ./configure allowing us to avoid #include <config.h> here. */
@HAVE_CLOCKID_T_DEFINE@

#ifndef HAVE_CLOCKID_T
typedef int clockid_t;
#else
#include <time.h>
#endif

#endif /* CLOCKID_T_H */
1 change: 1 addition & 0 deletions libutils/csv_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <csv_writer.h>

#include <alloc.h>
Expand Down
1 change: 0 additions & 1 deletion libutils/csv_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
* This writer implements CSV as in RFC 4180
*/

#include <platform.h>
#include <writer.h>

typedef struct CsvWriter_ CsvWriter;
Expand Down
4 changes: 0 additions & 4 deletions libutils/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
#ifndef CFENGINE_ENCODE_H
#define CFENGINE_ENCODE_H


#include <platform.h>


char *StringEncodeBase64(const char *str, size_t len);


Expand Down
1 change: 1 addition & 0 deletions libutils/json-yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <alloc.h>
#include <logging.h>
#include <misc_lib.h>
Expand Down
3 changes: 3 additions & 0 deletions libutils/json-yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

#include <json.h>

/* Set by ./configure allowing us to avoid #include <config.h> here. */
#define HAVE_LIBYAML 1

#ifdef HAVE_LIBYAML
#include <yaml.h>
#endif
Expand Down
53 changes: 53 additions & 0 deletions libutils/json-yaml.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2023 Northern.tech AS

This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/

#ifndef CFENGINE_JSON_YAML_H
#define CFENGINE_JSON_YAML_H

#include <json.h>

/* Set by ./configure allowing us to avoid #include <config.h> here. */
@HAVE_LIBYAML_DEFINE@

#ifdef HAVE_LIBYAML
#include <yaml.h>
#endif

/**
@brief Parse a YAML string to create a JsonElement
@param data [in Pointer to the string to parse
@param json_out Resulting JSON object
@returns See JsonParseError and JsonParseErrorToString
*/
JsonParseError JsonParseYamlString(const char **data, JsonElement **json_out);

/**
* @brief Convenience function to parse JSON from a YAML file
* @param path Path to the file
* @param size_max Maximum size to read in memory
* @param json_out Resulting JSON object
* @return See JsonParseError and JsonParseErrorToString
*/
JsonParseError JsonParseYamlFile(const char *path, size_t size_max, JsonElement **json_out);

#endif // CFENGINE_JSON_YAML_H
1 change: 1 addition & 0 deletions libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <logging.h>
#include <json.h>
#include <json-priv.h>
Expand Down
1 change: 1 addition & 0 deletions libutils/known_dirs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <known_dirs.h>
#include <definitions.h>
#include <file_lib.h>
Expand Down
2 changes: 1 addition & 1 deletion libutils/known_dirs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef CFENGINE_KNOWN_DIRS_H
#define CFENGINE_KNOWN_DIRS_H

#include <platform.h>
#include <path_max.h>

const char *GetDefaultDir_helper(char dir[PATH_MAX], const char *root_dir,
const char *append_dir);
Expand Down
1 change: 0 additions & 1 deletion libutils/libcrypto-compat.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef LIBCRYPTO_COMPAT_H
#define LIBCRYPTO_COMPAT_H

#include <platform.h>
#include <openssl/opensslv.h>


Expand Down
1 change: 1 addition & 0 deletions libutils/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <logging.h>
#include <alloc.h>
#include <string_lib.h>
Expand Down
5 changes: 3 additions & 2 deletions libutils/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
#ifndef CFENGINE_LOGGING_H
#define CFENGINE_LOGGING_H


#include <platform.h>
#include <compiler.h>
#include <stdbool.h>
#include <stdarg.h> /* va_list */
#include <stddef.h> /* size_t */
#include <time.h> /* struct tm */


// Does not include timezone, since it is hard to match on Windows.
Expand Down
1 change: 1 addition & 0 deletions libutils/man.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <man.h>

#include <string_lib.h>
Expand Down
2 changes: 1 addition & 1 deletion libutils/man.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef CFENGINE_MAN_H
#define CFENGINE_MAN_H

#include <platform.h>
#include <time.h> /* time_t */
#include <writer.h>

void ManPageWrite(Writer *out, const char *program, time_t last_modified,
Expand Down
5 changes: 2 additions & 3 deletions libutils/misc_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
#ifndef CFENGINE_MISC_LIB_H
#define CFENGINE_MISC_LIB_H

#include <platform.h>

#include <compiler.h>

#include <stddef.h> /* size_t */
#include <clockid_t.h>

#define ProgrammingError(...) __ProgrammingError(__FILE__, __LINE__, __VA_ARGS__)

Expand Down
1 change: 1 addition & 0 deletions libutils/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
included file COSL.txt.
*/

#include <platform.h>
#include <mutex.h>

#include <logging.h> /* Log */
Expand Down
2 changes: 1 addition & 1 deletion libutils/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef CFENGINE_MUTEX_H
#define CFENGINE_MUTEX_H

#include <platform.h>
#include <pthread.h>

#define THREAD_BLOCK_INDEFINITELY -1

Expand Down
3 changes: 2 additions & 1 deletion libutils/passopenfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <passopenfile.h> /* Starts with <platform.h> */
#include <platform.h>
#include <passopenfile.h>

#include <logging.h>
#include <alloc.h>
Expand Down
1 change: 0 additions & 1 deletion libutils/passopenfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
* transmission of a text message accompanying the descriptor).
*/

#include <platform.h>
/* Send a file descriptor to another process.
*
* @param uds The local communications socket.
Expand Down
24 changes: 24 additions & 0 deletions libutils/path.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
Copyright 2023 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/

#include <platform.h>
#include <path.h>
#include <string_lib.h>
#include <file_lib.h>
Expand Down
2 changes: 0 additions & 2 deletions libutils/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef CFENGINE_PATH_H
#define CFENGINE_PATH_H

#include <platform.h>

// TODO: Move more functions from files_names.c here

/**
Expand Down
Loading

0 comments on commit a97533a

Please sign in to comment.