Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to build minmea with meson #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*~
*.diff
.*.swp
minmea
tests
example
minmea/tests
minmea/example
*.exe
build

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary:

  • it may be named anything and a popular convention is builddir/, or builddir-release/ and builddir-debug/, so this gitignore entry likely won't take effect

  • Meson knows at the time of creating builddir/ that it is a build directory, and that no files in there should be added to git, so it creates builddir/.gitignore which ignores *, so that you don't have to. This just magically works.

    Admittedly, this was implemented in December 2020 via mesonbuild/meson@7437d98 and released in Meson 0.57.0, so back when you originally submitted this PR that was not the case, but still... it is long past December 2020, so you can remove this now. :)

20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ CFLAGS += -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_DARWIN_C_
CFLAGS += $(shell pkg-config --cflags check)
LDLIBS += $(shell pkg-config --libs check)

SRC_DIR = minmea

all: scan-build test example
@echo "+++ All good."""

test: tests
test: $(SRC_DIR)/tests
@echo "+++ Running Check test suite..."
./tests
./$(SRC_DIR)/tests

example: $(SRC_DIR)/example

scan-build: clean
@echo "+++ Running Clang Static Analyzer..."
scan-build $(MAKE) tests
scan-build $(MAKE) $(SRC_DIR)/tests

clean:
$(RM) tests example *.o
$(RM) $(SRC_DIR)/tests $(SRC_DIR)/example $(SRC_DIR)/*.o

tests: tests.o minmea.o
example: example.o minmea.o
tests.o: tests.c minmea.h
minmea.o: minmea.c minmea.h
$(SRC_DIR)/tests: $(SRC_DIR)/tests.o $(SRC_DIR)/minmea.o
$(SRC_DIR)/example: $(SRC_DIR)/example.o $(SRC_DIR)/minmea.o
$(SRC_DIR)/tests.o: $(SRC_DIR)/tests.c $(SRC_DIR)/minmea.h
$(SRC_DIR)/minmea.o: $(SRC_DIR)/minmea.c $(SRC_DIR)/minmea.h

.PHONY: all test scan-build clean
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ while (fgets(line, sizeof(line), stdin) != NULL) {
Simply add ``minmea.[ch]`` to your project, ``#include "minmea.h"`` and you're
good to go.

Alternatively for projects built with `meson`, add this repository in the
`subprojects` directory and add minmea_dep as a dependency of your project.

## Running unit tests

Building and running the tests requires the following:
Expand All @@ -133,6 +136,9 @@ Building and running the tests requires the following:
If you have both in your ``$PATH``, running the tests should be as simple as
typing ``make``.

Alternatively if using `meson` to build minmea, invoke `ninja test` in the
build directory to run the tests.

## Limitations

* Only a handful of frames is supported right now.
Expand Down
79 changes: 79 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
project(
'minmea',
'c',
default_options: ['c_std=c99', 'buildtype=debug'],
)

add_project_arguments(
'-Wall', '-Wextra', '-Werror',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meson will warn you not to do this, and suggest instead that you set default_options to include ['warning_level=2', 'werror=true'].

This will guarantee that it works cross-platform and on any compiler, and it also means that when GCC adds new warnings to -Wextra, which it often does, users can still build the project by passing -Dwerror=false as a manual override.

'-D_POSIX_C_SOURCE=199309L', '-D_BSD_SOURCE', '-D_DEFAULT_SOURCE',
'-D_DARWIN_C_SOURCE',
language: 'c',
)

if host_machine.system() == 'windows'
add_project_arguments('DMINMEA_INCLUDE_COMPAT')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is missing a leading -

endif

c_args = []

if get_option('buildtype') == 'debug'
c_args += '-ggdb'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug buildtype already sets -g, why do you need to enforce -ggdb (which the Makefile doesn't do anyway)?

endif

minmea_includes = include_directories('.')

minmea_sources = ['minmea/minmea.c']

minmea_dependencies = []

minmea_lib = library(
'minmea',
minmea_sources,
include_directories: minmea_includes,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current directory is an implicit include, you still need to use it for declare_dependency, but you don't need it for defining the library. Minus one line.

dependencies: minmea_dependencies,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an empty array, is it just future proofing? The project should confirm whether it's worth the additional lines for something that may have a definite development policy to never utilize.

In fact this seems likely to be the case, the project goals state that it is lightweight/minimal, and mention the bullet point:

  • One source file and one header - can't get any simpler.

So it sounds like they would not be willing to add dependencies in the future, which means you don't need to plan ahead for dependencies.

c_args: c_args,
)

minmea_dep = declare_dependency(
include_directories: minmea_includes,
dependencies: minmea_dependencies,
link_with: minmea_lib,
)

if get_option('example')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not bother with this option, just always define the example to be built. It's a single compile rule with no dependencies, it doesn't add any complexity to building, so there's no cost to always building it.

If you just don't want to run an extraneous build rule when using this as a wrap, set this executable to build_by_default: false.

minmea_example_sources = [
'minmea/example.c',
]

minmea_example_dependencies = [
minmea_dep,
]

minmea_example = executable(
'example',
minmea_example_sources,
dependencies: minmea_example_dependencies,
)
endif

if get_option('tests')
check = dependency('check')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a feature option for this these days, you can do check = dependency('check', required: get_option('tests')) and it will be a tristate lookup. If enabled, the dependency is required. If disabled, the dependency is skipped and forced to not-found. If auto, it will try to find the dependency, but allow not finding it.

Then, you can check whether the dependency was found and only when it is found, define the tests.


minmea_test_sources = [
'minmea/tests.c',
]

minmea_test_dependencies = [
check,
minmea_dep,
]

minmea_test = executable(
'tests',
minmea_test_sources,
dependencies: minmea_test_dependencies,
)

test('tests', minmea_test)
endif
2 changes: 2 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
option('tests', type: 'boolean', value: true)
option('example', type: 'boolean', value: true)
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions minmea.c → minmea/minmea.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>

Expand Down Expand Up @@ -85,10 +84,6 @@ bool minmea_check(const char *sentence, bool strict)
return true;
}

static inline bool minmea_isfield(char c) {
return isprint((unsigned char) c) && c != ',' && c != '*';
}

bool minmea_scan(const char *sentence, const char *format, ...)
{
bool result = false;
Expand Down
9 changes: 9 additions & 0 deletions minmea.h → minmea/minmea.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
extern "C" {
#endif

#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
Expand Down Expand Up @@ -252,6 +253,14 @@ static inline float minmea_tocoord(struct minmea_float *f)
return (float) degrees + (float) minutes / (60 * f->scale);
}

/**
* Check whether a character belongs to the set of characters allowed in a
* sentence data field.
*/
static inline bool minmea_isfield(char c) {
return isprint((unsigned char) c) && c != ',' && c != '*';
}

#ifdef __cplusplus
}
#endif
Expand Down
File renamed without changes.